In R, write each nested data frame to a CSV
Asked Answered
A

1

6

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?))
Aboutface answered 28/4, 2017 at 1:33 Comment(2)
I don't have experience with using map personally but after reading Hadley's R for Data Science book, I think this may be a case for map2 since you have two columns to use.Alrzc
yea probably! I don't knowAboutface
W
8

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.

Washable answered 28/4, 2017 at 1:48 Comment(3)
works awesome for my use case! thanks for quick reply!Aboutface
UPDATE: use purrrlyr:by_row() as it's no longer in purrrAboutface
Note that that .$data appears to pass a list of length 1 to the function, which caused an error in the function I was using (write_parquet). Updating to get rid of the list worked for me temp <- df %>% by_row(~write.csv(.$data[[1]], file = .$dataset))Supraliminal

© 2022 - 2024 — McMap. All rights reserved.