英文:
Merging tow dataframes in R that do not have common columns
问题
我一直在尝试合并两个在R中没有相同列的数据框。我得到的输出是一个具有额外的行和重复值的数据框。我的尝试如下,欢迎对代码进行编辑。
df1 <- data.frame(A=c("A","B", "C", "D", "E"),
B=c(7, 9, 8, 3, 2),
C=c(3, 5, 2, 9, 9))
df2 <- data.frame(D=c(1, 3, 3, 4, 5),
E=c(7, 7, 8, 3, 2))
df3 <- merge(x=df1, y=df2, by=NULL)
这个代码能够工作,但是输出给我一个具有25个观察和5个变量的数据框。有一个额外的行集,就像是一个重复。请注意,这里并不是整个数据集,只是一个子集。
A B C D E
1 A 7 3 1 7
2 B 9 5 1 7
3 C 8 2 1 7
4 D 3 9 1 7
5 E 2 9 1 7
6 A 7 3 3 7
7 B 9 5 3 7
8 C 8 2 3 7
我期望的输出是像下面这样的(5个观察和5个变量),其中我不需要额外的行集和重复的值。
A B C D E
1 A 7 3 1 7
2 B 9 5 3 7
3 C 8 2 3 8
4 D 3 9 4 3
5 E 2 9 5 2
英文:
I have been trying to merge two dataframes in r that do not have the same column. The output I get is one that has an extra set of rows and duplicated values. My attempt is below and edits to the code a welcome.
df1 <- data.frame(A=c("A","B", "C", "D", "E"),
B=c(7, 9, 8, 3, 2),
C=c(3, 5, 2, 9, 9))
df2 <- data.frame(D=c(1, 3, 3, 4, 5),
E=c(7, 7, 8, 3, 2))
df3<-merge(x=df1, y=df2, by=NULL)
This works but now the output gives me 25 observations of 5 variables. Where the is an extra set of rows like a duplication. ( disclaimer this is not the entire dataset here just a subset of it) .
A B C D E
1 A 7 3 1 7
2 B 9 5 1 7
3 C 8 2 1 7
4 D 3 9 1 7
5 E 2 9 1 7
6 A 7 3 3 7
7 B 9 5 3 7
8 C 8 2 3 7
My desired output is something like the one below (5 observations and 5 variables), where I do not an additional set of rows and duplicated values.
A B C D E
1 A 7 3 1 7
2 B 9 5 3 7
3 C 8 2 3 8
4 D 3 9 4 3
5 E 2 9 5 2
答案1
得分: 1
你可以使用 cbind() 来完成这个任务。
df3 <- cbind(df1, df2)
英文:
As some of the comments already mentioned you can use cbind() to accomplish that.
df3 <- cbind(df1, df2)
答案2
得分: 1
如 cbind
数据框方法只是data.frame(..., check.names = FALSE)
的一个包装器,您也可以使用:
df3 <- data.frame(df1, df2, check.names = FALSE)
#df3 <- data.frame(df1, df2) #替代方法
df3
# A B C D E
#1 A 7 3 1 7
#2 B 9 5 3 7
#3 C 8 2 3 8
#4 D 3 9 4 3
#5 E 2 9 5 2
英文:
As the cbind
data frame method is just a wrapper for data.frame(..., check.names = FALSE)
you can also use:
df3 <- data.frame(df1, df2, check.names = FALSE)
#df3 <- data.frame(df1, df2) #Alternative
df3
# A B C D E
#1 A 7 3 1 7
#2 B 9 5 3 7
#3 C 8 2 3 8
#4 D 3 9 4 3
#5 E 2 9 5 2
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论