首页 > 解决方案 > 如何合并两个data.frame并标记匹配是否找到

问题描述

我有两个 data.frame,df1 和 df2,如下所示:df1:

在此处输入图像描述

df2:

在此处输入图像描述

df1 和 df2 可以使用以下代码构建:

df1<-structure(list(Var = c("SEX", "SEXSP", "FEMCBP", "FEMCBPSP", 
"RACE", "RACESP", "ETHNIC", "INITVER", "IFCDT", "STDYPART"), 
    Label = c("Gender:", "If other, please specify:", "If female, please select one of the following:", 
    "If other, please specify:", "Race:", "If other, please specify:", 
    "Ethnicity:", "Version of protocol the subject consented to when subject started the study:", 
    "Date Informed Consent was signed by subject to start the study (DD MMM YYYY):", 
    "Study Arm:")), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))

df2<- structure(list(Var2 = c("RACE", "RACESP", "ETHNIC", "IFCDT", 
"STDYPART"), Label2 = c("Race:", "If other, please specify:", 
"Ethnicity:", "Date Informed Consent was signed by subject to start the study (DD MMM YYYY):", 
"Study Arm:")), row.names = c(NA, -5L), class = c("tbl_df", "tbl", 
"data.frame"))

我想将这两者合并在一起,看看我们是否可以在 df2 中找到 df1。我想得到看起来像这样的东西: 在此处输入图像描述

我应该怎么办?

df3<-merge(df1, df2, by.x=var, by.y=var2)

and?

标签: rmerge

解决方案


定义数据框后,编写下面的代码。all.x 表示通过键即by.x和by.y匹配后,从左表(x)中获取所有记录

df <- merge(df1,df2,by.x = "Var",by.y = "Var2",all.x = TRUE)

创建一个显示是否存在匹配的列

df$Matched <- ifelse(!is.na(df$Label2),"Y","N")

推荐阅读