Home > OS >  Matching a row across two columns in R
Matching a row across two columns in R

Time:09-15

I am trying to figure out how to add a new column that tells me whether or not 2 columns are matched. Dummy data set below:

col_A  col_B
food   food
drink  food
food   drink
drink  drink

What I want is something like this:

col_A  col_B  match_col
food   food   match
drink  food   no match
food   drink  no match
drink  drink  match

I am not sure if I need to a join or ifelse or combination of the two?

CodePudding user response:

You can use ==.

For two columns:

df |>
  transform(match_col = ifelse(col_A == col_B, "match", "no match"))

For multiple columns:

library(dplyr)
df %>% 
  mutate(match_col = if_all(col_B:col_D, `==`, col_A))

CodePudding user response:

Try to run this and see how it works. Not the most sophisticated code, but it is simple to understand.

col_A<-c("food","drink")
col_B<-c("food","food")
df<-data.frame(col_A,col_B)
res<-match(df$col_A,df$col_B)
res<-gsub(1,"match",res)
res[is.na(res)]<-"no match"
res
df<-data.frame(df,res)
df

CodePudding user response:

If your data frame is called mydf and you could live with TRUE and FALSE instead of "match" and "no match" there is this function called == as in

mydf$col_A == mydf$col_B

or

mydf$match_col <- mydf$col_A == mydf$col_B
  • Related