This a somewhat follow-up to this question. I want to use dplyr
functions instead of ddply
to apply a function that yields several rows which are directly included in the result. I guess this is best explained in the following example:
library(plyr)
#library(dplyr)
dfx <- data.frame(
group = c(rep('A', 8), rep('B', 15), rep('C', 6)),
sex = sample(c("M", "F"), size = 29, replace = TRUE),
age = runif(n = 29, min = 18, max = 54)
)
p <- c(.2,.4,.6,.8)
ddply(dfx, .(group), .fun = summarize, p=p, stats=quantile(age,probs=p))
# dfx %>% group_by(group) %>% do(p=p, stats=quantile(.$age, probs=p))
The ddply solutions looks like this (don't load dplyr
for this to work):
# group p stats
# 1 A 0.2 32.81104
# 2 A 0.4 34.13195
# 3 A 0.6 37.34055
# 4 A 0.8 44.21874
# 5 B 0.2 25.58858
# 6 B 0.4 34.67511
# 7 B 0.6 40.68370
# 8 B 0.8 44.67346
# 9 C 0.2 37.22625
# 10 C 0.4 42.46769
# 11 C 0.6 43.27065
# 12 C 0.8 44.54724
The dplyr
solution (the commented lines) yields the following:
# group p stats
# 1 A <dbl[4]> <dbl[4]>
# 2 B <dbl[4]> <dbl[4]>
# 3 C <dbl[4]> <dbl[4]>
Here, the data is "hidden" in the list elements. Is there a way to directly get the ddply
solution above?
(Note that I posted this question on the manipulatr mailing list, so far with no answer.)