This is a few repetitions of this question . I want to use functions dplyrinstead ddplyto apply a function that gives several lines that are directly included in the result. I think 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))
Ddply solutions look like this (don't download dplyrfor this):
# 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 solution dplyr(commented out lines) gives 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 items. Is there a way to directly get the solution ddplyabove? (Note that I posted this question on the manulatr mailing list , still unanswered.)