Ultimately, I am trying to achieve something similar to the following, but leveraging dplyr
instead of plyr
:
library(dplyr)
probs = seq(0, 1, 0.1)
plyr::ldply(tapply(mtcars$mpg,
mtcars$cyl,
function(x) { quantile(x, probs = probs) }))
# .id 0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100%
# 1 4 21.4 21.50 22.80 22.80 24.40 26.0 27.30 30.40 30.40 32.40 33.9
# 2 6 17.8 17.98 18.32 18.98 19.40 19.7 20.48 21.00 21.00 21.16 21.4
# 3 8 10.4 11.27 13.90 14.66 15.04 15.2 15.44 15.86 16.76 18.28 19.2
The best dplyr
equivalent I can come up with is something like this:
library(tidyr)
probs = seq(0, 1, 0.1)
mtcars %>%
group_by(cyl) %>%
do(data.frame(prob = probs, stat = quantile(.$mpg, probs = probs))) %>%
spread(prob, stat)
# cyl 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
# 1 4 21.4 21.50 22.80 22.80 24.40 26.0 27.30 30.40 30.40 32.40 33.9
# 2 6 17.8 17.98 18.32 18.98 19.40 19.7 20.48 21.00 21.00 21.16 21.4
# 3 8 10.4 11.27 13.90 14.66 15.04 15.2 15.44 15.86 16.76 18.28 19.2
Notice that I I also need to use tidyr::spread
. In addition, notice that I have lost the %
formatting for the column headers at the benefit of replacing .id
with cyl
in the first column.
Questions:
- Is there a better
dplyr
based approach to accomplishing thistapply %>% ldply
chain? - Is there a way to get the best of both
worlds without jumping through too many hoops? That is, get the
%
formatting and the propercyl
column name for the first column?