R - 如果满足文本条件,则合并来自两个具有不同 nrow 的数据帧的数据
R - Merge data from two data frames with different nrow if text condition is met
id1=c('1text','2text','3text')
df1=data.frame(id1)
df1
id1
1 1text
2 2text
3 3text
id2=c('2text','3text')
area2=c(11,22)
df2=data.frame(id2,area2)
df2
id2 area2
1 2text 11
2 3text 22
3 1text 33
我想将 area2 的数据添加到那些行中的数据框 df1,其中 id1=id2,因此我在 df1 中有一个包含该区域的新列。它应该是这样的:
id1 area1
1 1text NA
2 2text 11
3 3text 22
有人可以帮忙吗?
dplyr
的解决方案:
library(dplyr)
id1=c('1text','2text','3text')
df1=data.frame(id1)
id2=c('2text','3text','1text')
area2=c(11,22,33)
df2=data.frame(id1=id2,area2)
inner_join(df1,df2,by="id1")
id1=c('1text','2text','3text')
df1=data.frame(id1)
df1
id1
1 1text
2 2text
3 3text
id2=c('2text','3text')
area2=c(11,22)
df2=data.frame(id2,area2)
df2
id2 area2
1 2text 11
2 3text 22
3 1text 33
我想将 area2 的数据添加到那些行中的数据框 df1,其中 id1=id2,因此我在 df1 中有一个包含该区域的新列。它应该是这样的:
id1 area1
1 1text NA
2 2text 11
3 3text 22
有人可以帮忙吗?
dplyr
的解决方案:
library(dplyr)
id1=c('1text','2text','3text')
df1=data.frame(id1)
id2=c('2text','3text','1text')
area2=c(11,22,33)
df2=data.frame(id1=id2,area2)
inner_join(df1,df2,by="id1")