Hi!
Working my way through Advanced R, and I guess at some basic level I can understand why this doesn't work, but could someone explain why the methods below return a different output? Thank you!
library(purrr)
library(dplyr, warn.conflicts = FALSE)
mtcars %>%
group_by(cyl) %>%
summarise_if(is.double, mean)
#> # A tibble: 3 x 11
#> cyl mpg disp hp drat wt qsec vs am gear carb
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 4 26.7 105. 82.6 4.07 2.29 19.1 0.909 0.727 4.09 1.55
#> 2 6 19.7 183. 122. 3.59 3.12 18.0 0.571 0.429 3.86 3.43
#> 3 8 15.1 353. 209. 3.23 4.00 16.8 0 0.143 3.29 3.5
mtcars %>%
group_by(cyl) %>%
modify_if(is.double, mean) %>%
head(3)
#> # A tibble: 3 x 11
#> # Groups: cyl [1]
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 20.1 6.19 231. 147. 3.60 3.22 17.8 0.438 0.406 3.69 2.81
#> 2 20.1 6.19 231. 147. 3.60 3.22 17.8 0.438 0.406 3.69 2.81
#> 3 20.1 6.19 231. 147. 3.60 3.22 17.8 0.438 0.406 3.69 2.81
mtcars %>%
group_by(cyl) %>%
map_df(mean)
#> # A tibble: 1 x 11
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 20.1 6.19 231. 147. 3.60 3.22 17.8 0.438 0.406 3.69 2.81
Created on 2018-11-07 by the reprex package (v0.2.1)
Edit: I guess this comes down to an overall understand of when to actually prefer/use purrr.
Another example, when/why should I use one method over the other?
library(purrr)
library(dplyr, warn.conflicts = FALSE)
modify_if(mtcars, is.double, ~ .x * 2) %>% head (2)
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4 42 12 320 220 7.8 5.24 32.92 0 2 8 8
#> Mazda RX4 Wag 42 12 320 220 7.8 5.75 34.04 0 2 8 8
mutate_if(mtcars, is.double, ~ .x * 2) %>% head(2)
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> 1 42 12 320 220 7.8 5.24 32.92 0 2 8 8
#> 2 42 12 320 220 7.8 5.75 34.04 0 2 8 8
Created on 2018-11-07 by the reprex package (v0.2.1)