plyr package

plyr包

data(tips,package=”reshape2”)#加载数据集

library(plyr)

head(tips)

基础函数汇总:

aggregate(x=tipstip,by=list(tipssex),FUN=mean)

plyr包函数
ddply(.data=tips,#拆分计算的对象
    .variables="sex", #拆分的依据
    .fun=function(x){  #计算的函数
         mean(tips$tip)
         })

ddply()函数,参一是分组的对象,参二是分组的依据,参数三是对分组计算的函数

函数命名规则
a:array d:data.frame l:list

plyr包的主要函数:**ply 首字母代表输入,第二个字母代表输出

如:adply()函数 输入数组,输出数据框

例:

data <- as.matrix(iris[,-5])
result2 <- adply(.data=data,
                 .margins=2,
                 .fun=function(x){
                   each(max,min,median,sd)(x)
                 })

each()函数是在plyr包中,主要作用是合并多个函数成一个函数,返回一个向量

练习:

用iris数据,对每一种花做一个简单回归。

#定义回归函数
model <- function(x){
lm(Sepal.Length~Sepal.Width,data=x)
}
#对每一组花进行回归
models <- dlply(.data=iris,
.variables=”Species”,
.fun=model)
#计算每个模型的系数
result3 <- ldply(.data=models,
.fun=coef)

result3
     Species (Intercept) Sepal.Width
1     setosa    2.639001   0.6904897
2 versicolor    3.539735   0.8650777
3  virginica    3.906836   0.9015345
用普通函数对每一种花做简单回归,则需要做分割,计算,整合三个步骤
pieces <- split(iris,list(iris$Species))#因为lapply函数族的第一个参数是列表或向量
models <- lapply(pieces,model)
result <- lapply(models,coef)
do.call("rbind",result)#整合回归结果
结果:
           (Intercept) Sepal.Width
setosa        2.639001   0.6904897
versicolor    3.539735   0.8650777
virginica     3.906836   0.9015345

plyr包其他辅助函数:

上面提到的each函数
x <- rnorm(10)
each(max,min,mean,sd)(x)

colwise()函数对数据框的每一列进行计算,返回一个数据框
colwise(mean,is.numeric)(iris)#计算数值型的列

Warning: cannot remove prior installation of package ‘bit’ Warning: restored ‘bit’ package ‘bit64’ successfully unpacked and MD5 sums checked Warning: cannot remove prior installation of package ‘bit64’ Warning: restored ‘bit64’ package ‘bitops’ successfully unpacked and MD5 sums checked Warning: cannot remove prior installation of package ‘bitops’ Warning: restored ‘bitops’ package ‘data.table’ successfully unpacked and MD5 sums checked Warning: cannot remove prior installation of package ‘data.table’ Warning: restored ‘data.table’ package ‘farver’ successfully unpacked and MD5 sums checked package ‘filelock’ successfully unpacked and MD5 sums checked Warning: cannot remove prior installation of package ‘filelock’ Warning: restored ‘filelock’ package ‘gert’ successfully unpacked and MD5 sums checked package ‘glmnet’ successfully unpacked and MD5 sums checked package ‘lme4’ successfully unpacked and MD5 sums checked package ‘mathjaxr’ successfully unpacked and MD5 sums checked package ‘matrixStats’ successfully unpacked and MD5 sums checked Warning: cannot remove prior installation of package ‘matrixStats’ Warning: restored ‘matrixStats’ package ‘minqa’ successfully unpacked and MD5 sums checked package ‘nloptr’ successfully unpacked and MD5 sums checked packageplyr’ successfully unpacked and MD5 sums checked package ‘rbibutils’ successfully unpacked and MD5 sums checked package ‘RcppEigen’ successfully unpacked and MD5 sums checked package ‘RCurl’ successfully unpacked and MD5 sums checked Warning: cannot remove prior installation of package ‘RCurl’ Warning: restored ‘RCurl’ package ‘readr’ successfully unpacked and MD5 sums checked package ‘rjson’ successfully unpacked and MD5 sums checked Warning: cannot remove prior installation of package ‘rjson’ Warning: restored ‘rjson’ package ‘RSQLite’ successfully unpacked and MD5 sums checked Warning: cannot remove prior installation of package ‘RSQLite’ Warning: restored ‘RSQLite’ package ‘tidyr’ successfully unpacked
最新发布
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值