I have two dataframes:
> df1:
a b
1 1 2
2 2 3
3 2 4
4 3 4
5 4 4
> df2:
a b
1 1 1
2 1 2
3 2 3
4 3 4
5 5 5
Then merge df1 and df2 to get df3, please notice merge by=c("a","b")
df3<-merge(df1,df2)
> df3
a b
1 1 2
2 2 3
3 3 4
I would like to get the index of rows in df1 which are selected, and add a column call "label" in df1.
> df1:
a b label
1 1 2 TRUE
2 2 3 TRUE
3 2 4 FALSE
4 3 4 TRUE
5 4 4 FALSE
I tried this:
df1$label<-apply(df1,1,function (x) ifelse(nrow(merge(x,df3))>0,TRUE,FALSE))
got the wrong result and it's very slow since my df1 is very large. Is there any easy way? like is.element in vectors? Thank you.