r - 如何合并两个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?
解决方案
定义数据框后,编写下面的代码。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")
推荐阅读
- c - 未正确发送文件。插座。C
- javascript - 使用 Flow 复制 TypeScript 'as'
- oracle - 如何在 oracle 中在其列之一具有 Long 作为数据类型的表上运行 groupby - 非法使用 LONG 数据类型
- r - 海查特桑基
- apache - 如何在 Apache 2.4.2 上禁用 HTTP 压缩或 SSL 压缩
- java - Javafx - ListView - 当我单击一项时,fxml 文件中定义的节点文本消失
- c# - 接口描述使用此类的某些参数的方法
- c# - 分页时 Azure Easy Tables 缺少和重复数据
- postgresql - SQL:如何获得数据库的全局概览
- sql - 尝试使用 OR 语句实现 2 个外连接。语法不正确?