根据r中另一个数据框中的列组合选择一个数据框中的特定行

时间:2019-04-12 06:09:48

标签: r dataframe

我有两个数据帧df1和df2,其中df1有3列,df2有3列,我想将df1中的行与df2中的行进行匹配。 df1如下:

   df1<-data.frame("id"=c("12e","13e","14e"),"name"=c("sam","susan","alex"),"familyname"=c("son","hen","ken"),"Age"=1:3)
df2<-data.frame("name"=c("sam","susan"),"familyname"=c("son","hen"),"status"=c("married","married"))

我只想选择df1中具有相同名称和姓氏的行,并将婚姻状态附加到df1中。 我尝试了下面的代码,但没有得到满足。

df3<- df1 %>% filter(df1$name %in% df2$name & df1$familyname %in% df2$familyname)

我想要这样的输出:

name  familyname  Age  status
sam        son   1     married
susan      hen   2     married
alex       ken   3     divorce

0 个答案:

没有答案