Write multiple data frames to csv-file using purrr::map [duplicate]

与世无争的帅哥 提交于 2019-12-23 16:25:07

问题


PROBLEM:

I have a list of dataframes which should be written to disk as csv-files.

Assume this is the list of data frames:

dfs <- list(iris,
            mtcars)

WHAT DID NOT WORK:

I have tried to build the correct file names like this, but it did not work:

dfs %>% 
  map(~paste0("data-raw/", ., ".csv"))

I hoped that this bit would correctly give back the file names as strings. Instead, map matched each column and each value to the paste0 call.

I also tried the deparse(substitute(.)) trick, but the . was not reckognized correctly in the map call.

The next step would be to write the data frames (elements of dfs) as csv-files.

QUESTION:

How can I use purrr::map(or a similar appraoch) to write each data frame (each element of dfs) as csv-file to disk using write_csv?


回答1:


try this.

list(iris = iris, mtcars = mtcars) %>%
  names(.) %>%
  map(~ write_csv(dfs[[.]], paste0("data-raw/", ., ".csv")))


来源:https://stackoverflow.com/questions/47528368/write-multiple-data-frames-to-csv-file-using-purrrmap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!