In R, write each nested data frame to a CSV

时光总嘲笑我的痴心妄想 提交于 2019-12-24 02:25:34

问题


PROBLEM:

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]>

That is, I want 3 CSVs from above: one CSV for each tibble under "data".

I prefer tidyverse functions to lapply or similar.

POTENTIAL SOLUTION

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?))

回答1:


If you use purrr::by_row, you can access the dataset via .$dataset:

temp <- df %>% by_row(~write.csv(.$data, file = .$dataset))

This will save each tibble in a row to a separate file under the name of dataset.



来源:https://stackoverflow.com/questions/43670521/in-r-write-each-nested-data-frame-to-a-csv

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