Is purrr::map too magical?

Viewed 108

Applying functions in a loop fashion with purrr::map is super handy, but extrating objects by its indices seems "too magical" to me, for example, the r.squared from lm summary method. How does it work internally?

library(tidyverse)
data("mtcars")
mtcars %>% 
  nest(data = -c(vs)) %>% 
  mutate(model = map(data, ~lm(mpg ~ wt, data = .x)),
         summary = map(model, summary),
         r2 = map_dbl(summary, "r.squared"))

# # A tibble: 2 x 5
#      vs data               model  summary       r2
#   <dbl> <list>             <list> <list>     <dbl>
# 1     0 <tibble [18 x 10]> <lm>   <smmry.lm> 0.672
# 2     1 <tibble [14 x 10]> <lm>   <smmry.lm> 0.726
0 Answers
Related