R之ddlpy函数学习[转载]

转自:https://www.cnblogs.com/aloiswei/p/6032513.html

1.函数

ddply(.data, .variables, .fun = NULL, ..., .progress = "none",.inform = FALSE, .drop = TRUE, .parallel = FALSE, .paropts = NULL)

2.例子

# Summarize a dataset by two variables
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)
)
head(dfx)
 group sex      age
1     A   M 22.44750
2     A   M 52.92616
3     A   F 30.00443
4     A   M 39.56907
5     A   M 18.89180
6     A   F 50.81139
#Note the use of the '.' function to allow
# group and sex to be used without quoting
ddply(dfx, .(group, sex), summarize,mean = round(mean(age), 2),sd = round(sd(age), 2))
  group sex  mean    sd#运行结果
1     A   F 40.41 14.71
2     A   M 30.35 13.17
3     B   F 34.81 12.76
4     B   M 34.04 13.36
5     C   F 35.09 13.39
6     C   M 28.53  4.57

需要加载包

 library(plyr)

释义:也就是按照第二个参数进行分类应用第三个参数(函数处理),对group和sex均相同的分为一类,进行应用第三个参数funtion处理!。

原文地址:https://www.cnblogs.com/BlueBlueSea/p/10104924.html