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', ), rep('B', ), rep('C', )),
sex = sample(c("M", "F"), size = , replace = TRUE),
age = runif(n = , min = , max = )
)
head(dfx)
group sex age
A M 22.44750
A M 52.92616
A F 30.00443
A M 39.56907
A M 18.89180
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), ),sd = round(sd(age), ))
group sex mean sd#运行结果
A F 40.41 14.71
A M 30.35 13.17
B F 34.81 12.76
B M 34.04 13.36
C F 35.09 13.39
C M 28.53 4.57

需要加载包

 library(plyr)

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

上一篇:[Fiddler] 开启Fiddler抓包的时候产品报“证书错误”


下一篇:Makedown常用符号整理