0 votes
1 view
in R Programming by (36k points)

I want to compare each column of one dataframe with another dataframe column and print each resulting overlap to separate files.

I am executing the for loop to get the results:

for (i in colnames(df2)){ 

  ccc<-df1[grep(paste(df2[,i], collapse = "|"), df1$x), ]

  write.csv(ccc, file = paste(i, ".csv", sep=""))

}

But the above code is working for a small amount of dataset, if I pass a large amount of dataset then I am not getting the desired output.

1 Answer

0 votes
by (78.1k points)

I think the main problem is with the empty ""cells, which should be NA.

df2[df2 == ""] <- NA

Now, the grep should work with the lapply() in R programming:

invisible(lapply(names(df2), function(x) {

  rr <- df1[grep(paste0(df2[,x], collapse= "|"), df1$Species_name), ]

  write.csv(rr, file = paste(x, ".csv", sep=""))

}))

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...