medavis6
medavis6

Reputation: 863

For Loop Over List of Data Frames and Create New Data Frames from Every Iteration Using Variable Name

I cannot for the life of me figure out where the simple error is in my for loop to perform the same analyses over multiple data frames and output each iteration's new data frame utilizing the variable used along with extra string to identify the new data frame.

Here is my code:

john and jane are 2 data frames among many I am hoping to loop over and compare to bcm to find duplicate results in rows.

x <- list(john,jane)

for (i in x) {
  test <- rbind(bcm,i)
  test$dups <- duplicated(test$Full.Name,fromLast=T)
  test$dups2 <- duplicated(test$Full.Name)
  test <- test[which(test$dups==T | test$dups2==T),]
  newname <- paste("dupl",i,sep=".")
  assign(newname, test)
}

Thus far, I can either get the naming to work correctly without including the x data or the loop to complete correctly without naming the new data frames correctly.

Intended Result: I am hoping to create new data frames dupl.john and dupl.jane to show which rows are duplicated in comparison to bcm.

I understand that lapply() might be better to use and am very open to that form of solution. I could not figure out how to use it to solve my problem, so I turned to the more familiar for loop.


EDIT:

Sorry if I'm not being more clear. I have about 13 data frames in total that I want to run the same analysis over to find the duplicate rows in $Full.Name. I could do the first 4 lines of my loop and then dupl.john <- test 13 times (for each data frame), but I am purposely trying to write a for loop or lapply() to gain more knowledge in R and because I'm sure it is more efficient.

Upvotes: 0

Views: 1642

Answers (1)

Jaime Caffarel
Jaime Caffarel

Reputation: 2469

If I understand correctly based on your intended result, maybe using the match_df could be an option.

library(plyr)
dupl.john <- match_df(john, bcm)
dupl.jane <- match_df(jane, bcm)

dupl.john and dupl.jane will be both data frames and both will have the rows that are in these data frames and bcm. Is this what you are trying to achieve?

EDITED after the first comment

library(plyr)
l <- list(john, jane)
res <- lapply(l, function(x) {match_df(x, bcm, on = "Full.Name")} )
dupl.john <- as.data.frame(res[1])
dupl.jane <- as.data.frame(res[2])

Now, res will have a list of the data frames with the matches, based on the column "Full.Name".

Upvotes: 1

Related Questions