I wrote the following function, it works. However it is very slow when df1
has 1700 rows, and df2
has 70000 rows. Is there anyway to improve the efficiency?
rowcheck <- function(df1, df2){
apply(df1, 1, function(x) any(apply(df2, 1, function(y) all(y==x))))
}
An example I wrote this function to apply to is: I want to check whether each row in df1 is contained as a row in df2:
df1=data.frame(a=c(1:3),b=c("a","b","c"))
df2=data.frame(a=c(1:6),b=rep(c("a","b","c"),2))
For each row of df1, I want to check if it is contained as a row in df2. I want to return of the function to be a logical vector of length nrow(df1).
Thank you for your help.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…