Dataframe to excel file from a nested pattern

Viewed 203

I am trying to make separate excel files with data from a nested data frame (with attributes specified in file name). How can I achieve this?

library(tidyverse)
nested<- mtcars %>%  group_by(vs, disp) %>% nest()

What do you think?

1 Answers

We can use iwalk or imap

library(purrr)
library(openxlsx)
library(stringr)
imap(nested$data, ~ write.xlsx(.x, file = str_c('file', .y, '.xlsx')))

Or if we want to have the values of the groups as file name

pmap(nested, ~ write.xlsx(..3, 
          file = sprintf('file_%d_%d.xlsx', ..1, ..2)))
Related