Home > front end >  How to write multiple .csv files from a list of data frames into a new folder with the original file
How to write multiple .csv files from a list of data frames into a new folder with the original file

Time:04-03

As the subject suggests, I'm trying to write a list of data frames into a new folder so, that the original name stays same as the original file (and so that the original files don't get overwritten in the original folder!). Tried the code below, but simply didn't get the syntax working:

sapply(names(points_calculated), 
       function (x) write_csv(points_calculated[[x]], file=paste("./ExperimentFolder/PointsFolder", x)))

CodePudding user response:

Given that points_calculated is a list of data.frames, and the names(points_calculated) are the filename you want, you can do the following:

library(purrr)
points_calculated %>%
  iwalk(
    ~ readr::write_csv(.x, file = paste("./ExperimentFolder/PointsFolder", .y))
  )
)

imap() will iterate over your list, and perform the function after ~. .x is the element of the list (the data.frame), and .y is it's name.

  • Related