Ddply & # 8594; dplyr: .fun = sum multiple lines

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.)

+4
2

, : no set.seed

 dfx %>% group_by(group) %>% do(data.frame(p=p, stats=quantile(.$age, probs=p)))
Source: local data frame [12 x 3]
Groups: group

    group   p    stats
1      A 0.2 27.68069
2      A 0.4 35.36915
3      A 0.6 39.15223
4      A 0.8 46.41073
5      B 0.2 34.68378
6      B 0.4 37.22358
7      B 0.6 40.76185
8      B 0.8 44.48645
9      C 0.2 33.86023
10     C 0.4 36.30515
11     C 0.6 46.80672
12     C 0.8 52.82140
+7

, ( ) do() dplyr v 0.2, 0.1.3.

0,2 do() :

  • , ... .

  • , ... do() .

, . ?do () , v 0.2.

+4

Source: https://habr.com/ru/post/1545967/


All Articles