Home > Enterprise >  Save several data frames with different names with loop
Save several data frames with different names with loop

Time:06-29

I have three different data frames and I want all of them be saved with different names.

I got the data frames within a list as well as the names.

Unfortunately, it is not possible to run two loops simultaneously in R and if I embed one loop into the other it is just overwriting.

 for (f in dataframe_list){
                for (i in names){
                        write.csv(f, paste0(i, ".csv"), row.names = FALSE)
                }

        }

That is an example for my embedded version.

Does anyone has a clue how to solve it ?

CodePudding user response:

One loop (over data frame indices) should be enough.

for (i in seq_along(dataframe_list)) {
  write.csv(dataframe_list[[i]], paste0(i, '.csv'), row.names=F)
}

To name the csv files according to the names of the data frames:

for (df.name in names(dataframe_list)) {
  write.csv(dataframe_list[[df.name]], paste0(df.name, '.csv'), row.names=F)
}
  • Related