Reputation: 187
In a loop what is the best way to save the results of every iteration a dataframe?
The column names are always the same but the rbind has a problem that row names are duplicates
I tried cbind, rbind and merge but anyting worked.
Example:
dfiteration <- data.frame()
dframe <- data.frame(id = c(1,2), other = c(1,4))
for (i in 1:3) {
dframe1 <- dframe
dfiteration <- rbind(dfiteration, dframe1)
}
Upvotes: 1
Views: 63
Reputation: 2301
If you want to save each dataframe separately, you can store the iterations in a list:
dframe <- data.frame(id = c(1,2), other = c(1,4))
mylist <- list()
for (i in 1:3){
dframe1 <- dframe
mylist[[i]] <- dframe1
}
Upvotes: 0
Reputation: 5958
This code works fine for me. Surely you didn't give the data to reproduce your error.
Try using dfiteration <- base::rbind(dfiteration, dframe1)
to ensure that rbind
is not being masked by another package you have loaded. You could also post the entire results of sessionInfo()
so we can check what R version and packages are around.
In parallel, change the make.row.names
argument to FALSE
within rbind
and that might work.
dfiteration <- base::rbind(dfiteration, dframe1, make.row.names=FALSE)
Upvotes: 1