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
You could do:
df2[mapply(function(x,y) x%in%y ,df1,df2)]<-NA
x y
1 l
2 m
3 j
4 k
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
.