Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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 (107k 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=""))

}))

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...