I have several models stored as a list in a tibble. How do I use purrr::map()
to iterate functions like summary()
over this list of models? Ideally, summary()
should print to the console and not be stored as an additional column in the tibble.
library(tidyverse)
linear_models <- as_tibble(ToothGrowth) %>%
mutate(dose = factor(dose)) %>%
group_by(supp) %>%
nest() %>%
mutate(models = map(data, ~ lm(len ~ dose, .))) %>%
ungroup() %>%
select(1, 3)
linear_models
#> # A tibble: 2 x 2
#> supp models
#> <fct> <list>
#> 1 VC <lm>
#> 2 OJ <lm>
Created on 2022-10-20 with reprex v2.0.2
CodePudding user response:
Extract the models
and use map
library(purrr)
library(dplyr)
linear_models %>%
pluck(models) %>%
map(summary)
Or in base R
lapply(linear_models$models, summary)