Stataq
Stataq

Reputation: 2301

how to merge two data.frame and mark matched found or not

I have two data.frame, df1 and df2 that look like following: df1:

enter image description here

df2:

enter image description here

df1 and df2 can be build using code:

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

I would like to merge those two together and see whether we can find df1 in df2. I would like to get sth that looks like this: enter image description here

what should I do?

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

and?

Upvotes: 0

Views: 107

Answers (1)

Irfaan
Irfaan

Reputation: 155

After defining your data frames, write the code below. all.x means after matching it by the key i.e by.x and by.y, fetch all records from the left table (x)

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

Create a column which shows if there was a match

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

Upvotes: 1

Related Questions