用awk计算95% [英] Calculating 95th percentile with awk

查看:112
本文介绍了用awk计算95%的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是awk脚本的新手,希望对计算包含以下数据的文件的第95个百分位数值有帮助:

I'm new in awk scripting and would like to have some help in calculating 95th percentile value for a file that consist of this data:

0.0001357
0.000112
0.000062
0.000054
0.000127
0.000114
0.000136

我尝试过:

cat filename.txt | sort -n |
awk 'BEGIN{c=0} {total[c]=$1; c++;} END{print total[int(NR*0.95-0.5)]}'

但是当我将其与excel进行比较时,我似乎没有获得正确的值.

but I dont seem to get the correct value when I compare it to excel.

推荐答案

我不确定Excel是否执行某种加权百分位数,但是如果您实际上想要原始集中的某个数字,则您的方法应该可以正确进行四舍五入.

I am not sure if Excel does some kind of weighted percentile, but if you actually want one of the numbers that was in your original set, then your method should work correctly for rounding.

您可以像这样简化一点,但这是一回事.

You can simplify a little bit like this, but it's the same thing.

sort -n input.txt  | awk '{all[NR] = $0} END{print all[int(NR*0.95 - 0.5)]}'

这篇关于用awk计算95%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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