R 高于第99百分位的值的百分比

R 高于第99百分位的值的百分比,r,R,如何计算第99百分位以上的值之和。然后将其除以总值,得到第99百分位以上值的百分比。 例如,汽车 > summary(mtcars$hp) Min. 1st Qu. Median Mean 3rd Qu. Max. 52.0 96.5 123.0 146.7 180.0 335.0 > quantile(mtcars$hp, 0.99) 99% 312.99 > sum(mtcars$hp) [1] 4694

如何计算第99百分位以上的值之和。然后将其除以总值,得到第99百分位以上值的百分比。 例如,汽车

> summary(mtcars$hp)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   52.0    96.5   123.0   146.7   180.0   335.0 

> quantile(mtcars$hp, 0.99)
   99% 
312.99 

> sum(mtcars$hp)
[1] 4694

从这里开始,它就像把所有大于312.99的值相加,然后除以4694

您将获得一个向量,该向量指示一个值是否高于第99百分位,条件是
mtcars$hp>分位数(mtcars$hp,0.99)
,该向量可用于将可求和的
mtcars$hp
子集

sum(mtcars$hp[mtcars$hp > quantile(mtcars$hp, 0.99)]) / sum(mtcars$hp)
#[1] 0.0713677
要使其成为小数点后1位的百分比,请乘以100并使用
round
如下所示:

round(sum(mtcars$hp[mtcars$hp>quantile(mtcars$hp, 0.99)])/sum(mtcars$hp)*100,1)
#[1] 7.1

快速提问:我应该在代码中添加什么,使其成为小数点后1位的百分比。e、 g 7.1%