awk程序计算风速方差
我有以下格式的文件,格式如下所示 -
DATA THROUGH 2001 YRS JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ANN
BIRMINGHAM AL 58 8.1 8.7 9.0 8.2 6.8 6.0 5.7 5.4 6.3 6.2 7.2 7.7 7.1
HUNTSVILLE AL 34 9.1 9.4 9.8 9.2 7.9 6.9 6.1 5.8 6.7 7.3 8.1 9.0 7.9
MOBILE AL 53 10.1 10.3 10.7 10.1 8.7 7.5 6.9 6.7 7.7 8.0 8.9 9.6 8.8
MONTGOMERY AL 57 7.7 8.2 8.3 7.3 6.1 5.8 5.7 5.2 5.9 5.7 6.5 7.1 6.6
ANCHORAGE AK 48 6.4 6.8 7.1 7.3 8.5 8.4 7.3 6.9 6.7 6.7 6.4 6.3 7.1
ANNETTE AK 37 11.7 11.9 10.6 10.7 9.0 8.5 7.8 8.1 8.9 11.4 11.8 12.0 10.2
BARROW AK 68 11.9 11.3 11.3 11.5 12.0 11.5 11.7 12.4 13.2 13.3 12.4 11.7 12.0
BARTER IS. AK 33 15.1 14.4 13.7 12.0 12.7 11.6 10.9 11.8 13.2 14.8 14.9 13.9 13.2
BETHEL AK 43 14.5 14.8 13.8 12.9 11.5 11.0 10.7 11.0 11.6 12.3 13.2 13.7 12.6
BETTLES AK 26 5.9 6.3 7.1 7.5 7.2 6.8 6.6 6.2 6.4 6.4 5.8 5.7 6.5
我需要计算除 AK 代表的阿拉斯加之外的每个州的方差。 方差的公式为:nƩ(x^2) -(Ʃ x^2) / n(n-1)。
我现在拥有的只是一些代码,比如
BEGIN {FS ="\t"} {if ($2 != "AK")
{
sum1 = sum1 + ($4)^2
sum2 = sum2 + $4
n = n+1
if($2 != "AK")
{
sum1 = sum1 * n;
sum3 = sum1 - (sum2)^2
sum3 = sum3 / (n*(n-1));
}
}} END {}
BEGIN {FS ="\t"} {if ($2 != "AK")
{
sum1 = sum1 * n;
sum3 = sum1 - (sum2)^2
sum3 = sum3 / (n*(n-1));
}}
END {print "average " sum3}
我得到除以零的错误,
我对如何计算这个公式缺乏想法。任何有关分解代码 - 查找模式和/或完整代码的建议将不胜感激。
I have the below file formatted in the fashion shown below -
DATA THROUGH 2001 YRS JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ANN
BIRMINGHAM AL 58 8.1 8.7 9.0 8.2 6.8 6.0 5.7 5.4 6.3 6.2 7.2 7.7 7.1
HUNTSVILLE AL 34 9.1 9.4 9.8 9.2 7.9 6.9 6.1 5.8 6.7 7.3 8.1 9.0 7.9
MOBILE AL 53 10.1 10.3 10.7 10.1 8.7 7.5 6.9 6.7 7.7 8.0 8.9 9.6 8.8
MONTGOMERY AL 57 7.7 8.2 8.3 7.3 6.1 5.8 5.7 5.2 5.9 5.7 6.5 7.1 6.6
ANCHORAGE AK 48 6.4 6.8 7.1 7.3 8.5 8.4 7.3 6.9 6.7 6.7 6.4 6.3 7.1
ANNETTE AK 37 11.7 11.9 10.6 10.7 9.0 8.5 7.8 8.1 8.9 11.4 11.8 12.0 10.2
BARROW AK 68 11.9 11.3 11.3 11.5 12.0 11.5 11.7 12.4 13.2 13.3 12.4 11.7 12.0
BARTER IS. AK 33 15.1 14.4 13.7 12.0 12.7 11.6 10.9 11.8 13.2 14.8 14.9 13.9 13.2
BETHEL AK 43 14.5 14.8 13.8 12.9 11.5 11.0 10.7 11.0 11.6 12.3 13.2 13.7 12.6
BETTLES AK 26 5.9 6.3 7.1 7.5 7.2 6.8 6.6 6.2 6.4 6.4 5.8 5.7 6.5
I need to calculate the variance for every state except alaska represented by AK .
The formula for the variance is : nƩ(x^2) -(Ʃ x^2) / n(n-1).
All I have now is some code like
BEGIN {FS ="\t"} {if ($2 != "AK")
{
sum1 = sum1 + ($4)^2
sum2 = sum2 + $4
n = n+1
if($2 != "AK")
{
sum1 = sum1 * n;
sum3 = sum1 - (sum2)^2
sum3 = sum3 / (n*(n-1));
}
}} END {}
BEGIN {FS ="\t"} {if ($2 != "AK")
{
sum1 = sum1 * n;
sum3 = sum1 - (sum2)^2
sum3 = sum3 / (n*(n-1));
}}
END {print "average " sum3}
i get a division by zero error
I am short of ideas as to how to compute this formula . Any suggestions on breaking the code down - finding the pattersn and or complete code will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当状态不是 AK 时,这将对第 10 列的平方求和。然后使用你的公式。
This sums the squares of column 10 when the state is not AK. Then uses your formula.