Nesting and mapping with pipes seem to be an extremely viable workflow in the tidyverse philosophy for data.frame driven analysis. On the downside, it takes a bit of headbanging to get the hold of the syntax...
Inspired by the idea, when I came through this Coding in R: Nest and map your way to efficient code. All good, but was wondering if it's possible to streamline the workflow, in short combine the:
- Nesting isn’t scary and your data hasn’t disappeared and
- Map your nest,
into one line, instead of two steps.
For reproducibility, we can take another SO question: Using nest and purrr::map outside of mutate, it's possible to drop the cyl column easily, but instead, if I want to
- select some specific column say,
mpg,dispandvsfor4cylinder and - only
mpg,dispfor8cylinder and - drop/ unmodify everything related to
6cylinders and - fit a
lm()model with the selected varibles usingmap()family of functions and - save the models using something like
walk().
library(tidyverse)
mtcars %>%
split(.$cyl) %>%
map(~ .x %>% select(-cyl)) %>%
walk2(names(.), ~write_csv(.x, paste0(.y, '.csv')))
That worked as it should, but when I try to apply the aprroach with nest and map even without trying the goals 1-3, it throws error:
mtcars %>% group_by(cyl) %>% nest() %>% map(.$data, lm(.$mpg ~ .$disp + .$vs, .data))
Error: Index 1 must have length 1, not 10 Run
rlang::last_error()to see where the error occurred. Will be great if the solution uses the newly introducedacross()with dplyr 1.0.0.