I understand that by design, tidyr
does less than reshape2
: tidyr
never aggregates.
Is there a "right" way to replicate reshape2
's aggregation, in the sense of better following the tidyverse philosophy?
I usually combine a few dplyr verbs and then one from tidyr. I.e.:
To replicate
dcast(mtcars, gear~cyl, value.var = "disp", sum)
gear 4 6 8
1 3 120.1 483.0 4291.4
2 4 821.0 655.2 0.0
3 5 215.4 145.0 652.0
One can do
mtcars %>%
group_by(gear, cyl) %>%
summarise(disp = sum(disp)) %>%
spread(cyl, disp)
Source: local data frame [3 x 4]
Groups: gear [3]
gear `4` `6` `8`
* <dbl> <dbl> <dbl> <dbl>
1 3 120.1 483.0 4291.4
2 4 821.0 655.2 NA
3 5 215.4 145.0 652.0
I'll appreciate any insight on whether this is an optimal solution, and if it's not, what would be better and why