Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

looking up similarities between columns

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading