在R中,将每个嵌套数据框写入CSV [英] In R, write each nested data frame to a CSV
本文介绍了在R中,将每个嵌套数据框写入CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问题:
我有一个来自purrr
的数据帧的数据帧,并且想要将每个嵌套的数据帧写入CSV.
I have data frame of data frames from purrr
and want to write each nested data frame to a CSV.
> df
# A tibble: 3 × 2
dataset data
<chr> <list>
1 aab <tibble [681 × 60]>
2 aae <tibble [1,486 × 173]>
3 acm <tibble [3,496 × 139]>
也就是说,我要从上面获取3个CSV:数据"下的每个小标题一个CSV.
That is, I want 3 CSVs from above: one CSV for each tibble under "data".
相对于lapply
或类似版本,我更喜欢tidyverse函数.
I prefer tidyverse functions to lapply
or similar.
潜在的解决方案
我认为必须使用map()
或类似功能:
I think it's gotta be something using map()
or similar function:
df %>%
map(~write_csv(data, file=[how to get filename from 'dataset' column?))
推荐答案
如果使用purrr::by_row
,则可以通过.$dataset
访问dataset
:
If you use purrr::by_row
, you can access the dataset
via .$dataset
:
temp <- df %>% by_row(~write.csv(.$data, file = .$dataset))
这会将行列中的每个小标题保存为 dataset 名称下的单独文件.
This will save each tibble in a row to a separate file under the name of dataset.
这篇关于在R中,将每个嵌套数据框写入CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文