ddply按组分配多个分位数 [英] ddply multiple quantiles by group

查看:76
本文介绍了ddply按组分配多个分位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何进行计算:

library(ddply)
quantile(baseball$ab)
  0%  25%  50%  75% 100% 
  0   25  131  435  705 

按组,按团队"说?我想要一个data.frame,其行名为"team",列名为"0%25%50%75%100%",即每组一个quantile调用.

by groups, say by "team"? I want a data.frame with rownames "team" and column names "0% 25% 50% 75% 100%", i.e. one quantile call per group.

ddply(baseball,"team",quantile(ab))

不是正确的解决方案.我的问题是每个分组操作的输出在这里都是长度为5的向量.

is not the correct solution. my problem is that the OUTPUT of each grouped operation is a vector of length 5 here.

换句话说,对此有什么巧妙的解决方案(不要管标题):

in other words, what's a neat solution to this (nevermind the header):

m=data.frame()
for (i in unique(baseball$team)){m=rbind(m,quantile(baseball[baseball$team==i, ]$ab))}
head(m,3)
  X120 X120.1 X120.2 X120.3 X120.4
1  120  120.0  120.0 120.00    120
2  162  162.0  162.0 162.00    162
3   89   89.0   89.0  89.00     89

推荐答案

使用基础R,您可以使用tapplydo.call

With base R you could use tapply and do.call

library(plyr)
do.call("rbind", tapply(baseball$ab, baseball$team, quantile))

do.call("rbind", tapply(baseball$ab, baseball$team, quantile, c(0.05, 0.1, 0.2)))

或者,使用ddply

ddply(baseball, .(team), function(x) quantile(x$ab))

这篇关于ddply按组分配多个分位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆