使用分组变量计算值的第95个百分位数 [英] Calculate 95th percentile of values with grouping variable

查看:81
本文介绍了使用分组变量计算值的第95个百分位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为分水岭分组的多个水质值计算第95个百分位数,例如:

I'm trying to calculate the 95th percentile for multiple water quality values grouped by watershed, for example:

Watershed   WQ
50500101    62.370661
50500101    65.505046
50500101    58.741477
50500105    71.220034
50500105    57.917249

我查看了发布的问题-每个观察变量的百分位数w/r/t分组变量.似乎非常接近我想做的事情,但这是为了每次观察.每个分组变量都需要它.所以很理想,

I reviewed this question posted - Percentile for Each Observation w/r/t Grouping Variable. It seems very close to what I want to do but it's for EACH observation. I need it for each grouping variable. so ideally,

Watershed   WQ - 95th
50500101    x
50500105    y

推荐答案

这可以使用 plyr 库来实现.我们指定分组变量 Watershed ,并要求WQ的95%分位数.

This can be achieved using the plyr library. We specify the grouping variable Watershed and ask for the 95% quantile of WQ.

library(plyr)
#Random seed
set.seed(42)
#Sample data
dat <- data.frame(Watershed = sample(letters[1:2], 100, TRUE), WQ = rnorm(100))
#plyr call
ddply(dat, "Watershed", summarise, WQ95 = quantile(WQ, .95))

和结果

  Watershed     WQ95
    1         a 1.353993
    2         b 1.461711

这篇关于使用分组变量计算值的第95个百分位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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