Reputation: 821
I am getting different results with the map function from the library purr. Here is the following code used in Hadley's book, R for data science. Why is this?
code1
models1 <- mtcars %>% split(.$cyl) %>%
map(function(df) lm(mpg ~ wt, data=mtcars))
models1 %>%
map(summary) %>%
map_dbl(~.$r.squared)
code1- result
4 6 8
0.5086326 0.4645102 0.4229655
code2
models2 <- mtcars %>% split(.$cyl) %>%
map(~lm(mpg ~ wt, data=.))
models2 %>%
map(summary) %>%
map_dbl(~.$r.squared)
code2-result
4 6 8
0.7528328 0.7528328 0.7528328
Upvotes: 0
Views: 64
Reputation: 2101
The problem is that you are passing the entire dataset mtcars
to the lm()
function in code-1 instead of passing in df
which you declare in your function definition in map(function(df) ...)
if you change mtcars
to df
your problem is fixed
library(tidyverse)
models1 <- mtcars %>% split(.$cyl) %>%
map(function(df) lm(mpg ~ wt, data=df))
models1 %>%
map(summary) %>%
map_dbl(~.$r.squared)
#> 4 6 8
#> 0.5086326 0.4645102 0.4229655
models2 <- mtcars %>% split(.$cyl) %>%
map(~lm(mpg ~ wt, data=.))
models2 %>%
map(summary) %>%
map_dbl(~.$r.squared)
#> 4 6 8
#> 0.5086326 0.4645102 0.4229655
Created on 2019-02-26 by the reprex package (v0.2.1)
Upvotes: 1