使用purrr :: map将多个数据帧写入csv文件 [英] Write multiple data frames to csv-file using purrr::map

查看:100
本文介绍了使用purrr :: map将多个数据帧写入csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

我有一个数据帧列表,应该以csv文件的形式写入磁盘.

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)

不起作用的地方

我试图像这样构建正确的文件名,但是没有用:

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

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

我希望此位可以正确地将文件名作为字符串返回.而是,map将每个列和每个值匹配到paste0调用.

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.

我也尝试了deparse(substitute(.))技巧,但是在map调用中无法正确识别..

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

下一步是将数据帧(dfs的元素)写为csv文件.

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

问题:

如何使用purrr::map(或类似方法)使用write_csv将每个数据帧(dfs的每个元素)作为csv文件写入磁盘?

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?

推荐答案

尝试一下.

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

这篇关于使用purrr :: map将多个数据帧写入csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆