【问题标题】:R - Merge data from two data frames with different nrow if text condition is met [duplicate]R - 如果满足文本条件,则合并来自具有不同 nrow 的两个数据帧的数据
【发布时间】:2021-09-30 11:13:21
【问题描述】:
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

有人可以帮忙吗?

【问题讨论】:

  • merge(df1, df2, by.x = 'id1', by.y = 'id2')

标签: r merge character


【解决方案1】:

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")

【讨论】:

猜你喜欢
  • 2016-10-06
  • 1970-01-01
  • 2021-08-08
  • 2019-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多