I am starting to incorporate the purrr package in my daily work. One thing I can't accomplish is splitting a dataframe into multiple dataframes. Following the example of purrr, I can do this:
mtcars %>%
split(mtcars$gear)
Which splits the dataframe into three smaller dataframes by "gear".
Now I would like to split the dataframe again, for instance by "am".
I have tried this:
mtcars %>%
split(mtcars$gear) %>%
map(split, mtcars$am)
which works, but throws a warning.
Following purrr to fit a model, I tried this, but I got an error:
mtcars %>%
split(mtcars$gear) %>%
map(split, mtcars$am) %>%
map(\(df) lm(mpg ~ wt, data = df)) |>
map(summary) %>%
map_dbl("r.squared")
"object 'wt' not found".
How could I fix this?