微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

以不规则的间隔计算平均值而不考虑shell脚本中的缺失值?

我有一个数据集与许多缺less值为-999。 部分数据是

input.txt 30 -999 10 40 23 44 -999 -999 31 -999 54 -999 -999 -999 -999 -999 -999 10 23 2 5 3 8 8 7 9 6 10 and so on

我想计算每个5,6,6行间隔的平均值,而不考虑缺失值。

欲望输出

ofile.txt 25.75 (ie consider first 5 rows and take average without considering missing values,so (30+10+40+23)/4) 43 (ie consider next 6 rows and take average without considering missing values,so (44+31+54)/3) -999 (ie consider next 6 and take average without considering missing values. Since all are missing,so write as a missing value -999) 8.6 (ie consider next 5 rows and take average (10+23+2+5+3)/5) 8 (ie consider next 6 rows and take average)

如果它是有规律的间隔(让我们说5),我可以做到这一点

find一个模式并修改下一行而不修改文件中的其他内容。 最好是基于Linux的命令(sed,awk等)

在Linux上“快速select”(或类似)的实现? (而不是sort | uniq -c | sort -rn | head – $ N)

将命令行parameter passing给shell脚本中的awk

awk – 如何删除字段分隔符的第一列

循环前缀的文件名到每个行的开始分隔标签文件(从而生成一个新的列))

awk '!/-999/{sum += $1; count++} NR%5==0{print count ? (sum/count) :-999;sum=count=0}' input.txt

在这里定期询问类似的问题计算平均值,而不考虑shell脚本中的缺失值? 但在这里,我要求解决不规则的时间间隔。

如何根据AWK / UNIX中的公共字段将行中的分隔字段合并为一个字段

如何使用任何Linux工具打印FILENAME前面的每个文件的第n行(第5行)?

Bash修改CSV来改变一个字段

删除列1具有特定数字条目的所有行

使用pipe道input一个awk语句

AWK

awk -vf="5" 'f&&f--&&$0!=-999{c++;v+=$0} NR%17==0{f=5;r++} !f&&NR%17!=0{f=6;r++} r&&!c{print -999;r=0} r&&c{print v/c;r=v=c=0} END{if(c!=0)print v/c}' input.txt

产量

25.75 43 -999 8.6 8

分解

f&&f--&&$0!=-999{c++;v+=$0} #add valid values and increment count NR%17==0{f=5;r++} #reset to 5,6 pattern !f&&NR%17!=0{f=6;r++} #set 6 if pattern doesnt match r&&!c{print -999;r=0} #print -999 if no valid values r&&c{print v/c;r=v=c=0} #print avg END{ if(c!=0) #print remaining values avg print v/c }

$ cat tst.awk function nextInterval( intervals) { numIntervals = split("5 6 6",intervals) intervalsIdx = (intervalsIdx % numIntervals) + 1 return intervals[intervalsIdx] } BEGIN { interval = nextInterval() noval = -999 } $0 != noval { sum += $0 cnt++ } ++numRows == interval { print (cnt ? sum / cnt : noval) interval = nextInterval() numRows = sum = cnt = 0 } $ awk -f tst.awk file 25.75 43 -999 8.6 8

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐