Comparing two similar dataframes and finding different values between them

前端 未结 1 1903
灰色年华
灰色年华 2021-01-20 09:34

This is a seemingly basic question, I apologize in advance if this is a duplicate question. I looked around and didn\'t see anything.

I have two dataframes full of

相关标签:
1条回答
  • 2021-01-20 10:02

    You could do:

    df2[mapply(function(x,y)   x%in%y ,df1,df2)]<-NA
         x    y
    1 <NA>    l
    2 <NA>    m
    3    j <NA>
    4    k <NA>
    

    This affects df2 directly, better have a copy of it.

    Explanation:
    mapply() is used to have the %in% applied between the first column of df1 and df2, and then the second and so on if there were more.
    This gives:

    > mapply(function(x,y)   x%in%y,df1,df2)
             x     y
    [1,]  TRUE FALSE
    [2,]  TRUE FALSE
    [3,] FALSE  TRUE
    [4,] FALSE  TRUE
    

    TRUE are the values that matched, these are the want we want to change into NA's.

    0 讨论(0)
提交回复
热议问题