以1:2列比率合并2个数据帧

时间:2016-06-30 10:05:26

标签: r merge

我有两个数据框

df1=data.frame(w=c(10,'a','a',14,''),data='other stuff')
df2=data.frame(c=10:14,n=letters[1:5],data='stuff')
> df1;df2
   w        data
1 10 other stuff
2  a other stuff
3  a other stuff
4 14 other stuff
5    other stuff
   c n  data
1 10 a stuff
2 11 b stuff
3 12 c stuff
4 13 d stuff
5 14 e stuff

我想制作一个看起来像(手工制作)的最终df:

10    stuff    other stuff
a     stuff    other stuff
a     stuff    other stuff
14    stfff    other stuff
      stuff    other stuff

我试过

merge(df1,df2,by.x='w',by.y='c|n')

无济于事,我不知道如何解决这个问题。请注意,df1和df2是48维×1000s

1 个答案:

答案 0 :(得分:3)

我们可以转换df2使一个键列与df1匹配,然后使用merge:

#dummy data updated data columns
df1 = data.frame(w = c(10,'a','a',14,''), data = paste('otherStuff', 1:5))
df2 = data.frame(c = 10:14, n = letters[1:5], data = paste('stuff', 1:5))

df1;df2

#    w         data
# 1 10 otherStuff 1
# 2  a otherStuff 2
# 3  a otherStuff 3
# 4 14 otherStuff 4
# 5    otherStuff 5

#    c n    data
# 1 10 a stuff 1
# 2 11 b stuff 2
# 3 12 c stuff 3
# 4 13 d stuff 4
# 5 14 e stuff 5


library(dplyr)
library(tidyr)

merge(df1,
      gather(df2, key = "Group", value = "w", -data),
      by = "w", all.x = TRUE)


#    w       data.x  data.y Group
# 1    otherStuff 5    <NA>  <NA>
# 2 10 otherStuff 1 stuff 1     c
# 3 14 otherStuff 4 stuff 5     c
# 4  a otherStuff 2 stuff 1     n
# 5  a otherStuff 3 stuff 1     n