I have a df like this.
df <- data.frame(
b1 = c(2, 6, 3, 80, 4, 77, 1, 9, NA),
b2 = c(100, 3, 9, 102, 6, 12, 1, 7, 77))
I want to compare column b1 with column b2 for similarity. So if any number in b1 can be found in anywhere in b2 create a new column (new) and write YES.
The idea is to have something like this as my result.
df <- data.frame(
b1 = c(2, 6, 3, 80, 4, 77, 1, 9, NA),
b2 = c(100, 3, 9, 102, 6, 12, 1, 7, 77),
new = c('NO', 'YES', 'YES', 'NO', 'NO', 'YES', 'YES', 'YES', 'NO'))
I have tried unsuccessfully to work this out using this.
df$new <- ifelse(df$b1 == df$b2), YES, NO)
>Solution :
Use %in%
:
df$b1 %in% df$b2
#[1] FALSE TRUE TRUE FALSE FALSE TRUE TRUE TRUE FALSE
ifelse(df$b1 %in% df$b2, "YES", "NO")
#[1] "NO" "YES" "YES" "NO" "NO" "YES" "YES" "YES" "NO"