I'm new to R and trying to understand how to use map and map_df. Consider the following:
iris %>% split(.$Species) %>% map_df(function (x) apply(x[, 1:4], 2, mean))
And compare to
iris %>% split(.$Species) %>% map(function (x) apply(x[, 1:4], 2, mean)) %>% as.data.frame
The former gives the following output:
Sepal.Length Sepal.Width Petal.Length Petal.Width
<dbl> <dbl> <dbl> <dbl>
1 5.01 3.43 1.46 0.246
2 5.94 2.77 4.26 1.33
3 6.59 2.97 5.55 2.03
The latter gives:
setosa versicolor virginica
Sepal.Length 5.006 5.936 6.588
Sepal.Width 3.428 2.770 2.974
Petal.Length 1.462 4.260 5.552
Petal.Width 0.246 1.326 2.026
My question is: why? I would expect these two commands to give the same output. How can I get the second output with map_df function?