vendredi 21 juin 2019

R ifelse based on an observation meeting two criteria

Yeah really simple problem just about to drive me over the deep end as I know there's a logical approach. I have two dataframes, and want to make a new column in the first dataframe 0/1 depending if any rows in the second contain a col1/col2 pair from the first.

Data

df.ex1 <- data.frame('name'=c('sally', 'joe', 'ben', 'nick'), 'grade1'=c('A', 'B', 'F', 'A'))
df.ex2 <- data.frame('name'=c('jed', 'ben', 'sally', 'nick'), 'grade1'=c('A', 'F', 'A', 'C'))

> df.ex1
   name grade1
1 sally      A
2   joe      B
3   ben      F
4  nick      A

> df.ex2
   name grade1
1   jed      A
2   ben      F
3 sally      A
4  nick      C

#Expected result:

       name grade1 bin
    1 sally      A  1
    2   joe      B  0
    3   ben      F  1
    4  nick      A  0

The obvious approach would be to check for presence of name-grade pair in second df:

df.ex1$bin <- ifelse(df.ex1[,1:2] %in% df.ex2[,1:2], 1, 0)

But this doesn't work. Why not? What's the correct approach? And actually what's the correct thought process to arrive at the correct approach?

Note, this obviously won't work:

df.ex1$bin <- ifelse(df.ex1[,1] %in% df.ex2[,1] & df.ex1[,2] %in% df.ex2[,2], 1, 0)

Aucun commentaire:

Enregistrer un commentaire