Note: The title of this question has been edited to make it the canonical question for issues when plyr
functions mask their dplyr
counterparts. The rest of the question remains unchanged.
Suppose I have the following data:
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)
)
With the good old plyr
I can create a little table summarizing my data with the following code:
require(plyr)
ddply(dfx, .(group, sex), summarize,
mean = round(mean(age), 2),
sd = round(sd(age), 2))
The output look like this:
group sex mean sd
1 A F 49.68 5.68
2 A M 32.21 6.27
3 B F 31.87 9.80
4 B M 37.54 9.73
5 C F 40.61 15.21
6 C M 36.33 11.33
I'm trying to move my code to dplyr
and the %>%
operator. My code takes DF then group it by group and sex and then summarise it. That is:
dfx %>% group_by(group, sex) %>%
summarise(mean = round(mean(age), 2), sd = round(sd(age), 2))
But my output is:
mean sd
1 35.56 9.92
What am I doing wrong?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…