Explore Courses Blog Tutorials Interview Questions
0 votes
in R Programming by (50.2k 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 (108k 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!

30.5k questions

32.6k answers


108k users

Browse Categories