计算多个列的平均值 [英] Calculate the average over a number of columns

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

问题描述

我正在尝试创建一个脚本,该脚本计算行数的平均值.

I am trying to create a script which calculates the average over a number of rows.

此数字将取决于我拥有的样本数量,该数量有所不同.

This number would depend on the number of samples that I have, which varies.

这些文件的示例在这里:

An example of these files is here:

24  1  2.505
24  2  0.728
24  3  0.681
48  1  2.856
48  2  2.839
48  3  2.942
96  1  13.040
96  2  12.922
96  3  13.130
192 1  50.629
192 2  51.506
192 3  51.016

平均值在第三列上计算,并且

第二列表示样本数,在这种情况下为3.

the second column indicates the number of samples, 3 in this particular case.

因此,我应该在这里获取 4个值.

Therefore, I should obtain 4 values here.

每3行1个平均值.

我尝试过类似的事情:

count=3;
total=0; 

for i in $( awk '{ print $3; }' ${file} )
do 
    for j in 1 2 3
    do
    total=$(echo $total+$i | bc )
    done
    echo "scale=2; $total / $count" | bc
done

但这并没有给我正确的答案,相反,我认为它计算出每三行一组的平均值.

But it is not giving me the right answer, instead I think it calculates an average per each group of three rows.

平均值在第三列上计算,并且

第二列表示样本数,在这种情况下为3.

the second column indicates the number of samples, 3 in this particular case.

因此,我应该在这里获取 4个值.

Therefore, I should obtain 4 values here.

每3行1个平均值.

我尝试过类似的事情:

count=3;
total=0; 

for i in $( awk '{ print $3; }' ${file} )
do 
    for j in 1 2 3
    do
    total=$(echo $total+$i | bc )
    done
    echo "scale=2; $total / $count" | bc
done

但这并没有给我正确的答案,相反,我认为它计算出每三行一组的平均值.

But it is not giving me the right answer, instead I think it calculates an average per each group of three rows.

预期产量

24  1.3046      
48  2.879       
96  13.0306     
192 51.0503     

推荐答案

显然,我提出了第三个观点.在awk中:

Apparently I brought a third view to the problem. In awk:

$ awk 'NR>1 && $1!=p{print p, s/c; c=s=0} {s+=$3;c++;p=$1} END {print p, s/c}' file
24 1.30467
48 2.879
96 13.0307
192 51.0503

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

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