Good job coming up with a reproducible example on your very first question!
I think the nested data.frame set-up works well with the pmap()
/pwalk()
variants of the map family. These functions are handy for rowwise work, where you want to go through each row of a data.frame and do something with the values in the columns. Here you want to go through each row of the nested data frame and save the data
given the country
name.
I would use pwalk()
, since you don't want anything returned within R but instead are saving something and walk()
variants are ...for functions called primarily for their side-effects; it returns .x
invisibly.
The code would then look like:
gapminder %>%
nest(-country) %>%
pwalk(~write_csv(x = .y, path = paste0(.x, ".csv") ) )
Your data
column is the second column (the .y
element), so is passed to the first argument in write_csv()
. The first column (the .x
element) contains the country names, which I paste together with .csv
to make the names of the datasets that are saved.