Search code examples
r

How to get common values between two columns in R?


Dataframe as an example:

df <- data.frame(x = c("A,B,C","A,D","B,C,E","C,E,G"),
                 y = c("A","D","A",NA),
                 MyAim = c("A","D","",""))

      x    y MyAim
1 A,B,C    A     A
2   A,D    D     D
3 B,C,E    A      
4 C,E,G <NA>      

I want to get common values between x and y columns in a new one. Thanks in advance.


Solution

  • We can use mapply :

    df$Z <- mapply(function(x, y) {
                temp <- intersect(x, y)
                if(length(temp)) temp else ""
            }, strsplit(df$x, ","), df$y)
    
    df
    #      x    y Z
    #1 A,B,C    A A
    #2   A,D    D D
    #3 B,C,E    A  
    #4 C,E,G <NA>  
    

    If there are multiple values in y, we can split the string in y and return a comma-separated value.

    df$Z <- mapply(function(x, y) {
         temp <- intersect(x, y)
         if(length(temp)) toString(temp) else ""
         }, strsplit(df$x, ","), strsplit(df$y, ","))
    

    data

    df <- data.frame(x = c("A,B,C","A,D","B,C,E","C,E,G"),
                     y = c("A","D","A",NA),
                     stringsAsFactors = FALSE)