我有一个10年(1995-2004)的时间序列每日数据集,其中一些缺失值为9999.00。 我想计算每年的年平均值,而不考虑缺失值。
我可以通过以下命令考虑365天日历来实现它
awk '!/\9999.00/{sum += $1; count++} NR%365==0{print count ? (sum) :9999.00;sum=count=0}'ifile但我无法用闰年日历修改。 我还需要添加另一列多年。 我的愿望输出是
1995 annual_average 1996 annual_average 1997 annual_average ....例如:我有以下1995 - 2000年的数据。 如果是闰年,我需要计算每3行而不是365行和4行的平均值而不是366行:
3 3 4 9999.00 4 9999.00 13 3 9999.00 9999.00 9999.00 9999.00 9999.00 3 4 2 2 2.6 5.1 4.5试用命令:
awk '!/\9999.00/{sum += $1; count++} NR%3==0{print count ? (sum) :9999.00;sum=count=0}'ifile欲望输出:
1995 3.33 1996 8.5 it is a leap year, so average of 4 lines without considering missing values (4+13)/2 1997 3 1998 9999.00 1999 3 2000 3.55 leap yearI have a time series daily dataset for 10 years (1995-2004) with some missing values as 9999.00. I would like to compute annual average for each year without considering the missing value.
I could able to make it by considering 365 days calendar with following command
awk '!/\9999.00/{sum += $1; count++} NR%365==0{print count ? (sum) :9999.00;sum=count=0}'ifileBut I can't able to modify with leap year calendar. I also need to add another column with years. My desire output is as
1995 annual_average 1996 annual_average 1997 annual_average ....For example: I have following data from 1995-2000. I need to compute average of every 3 lines instead of 365 and 4 lines instead of 366 if it is a leap year:
3 3 4 9999.00 4 9999.00 13 3 9999.00 9999.00 9999.00 9999.00 9999.00 3 4 2 2 2.6 5.1 4.5Trial command:
awk '!/\9999.00/{sum += $1; count++} NR%3==0{print count ? (sum) :9999.00;sum=count=0}'ifileDesire output:
1995 3.33 1996 8.5 it is a leap year, so average of 4 lines without considering missing values (4+13)/2 1997 3 1998 9999.00 1999 3 2000 3.55 leap year最满意答案
此代码适用于您的示例数据。 当然,您需要调整target :
BEGIN { year = 0; target = 3; } $1 < 9990.00 { sum += $1; count++; } NR == target { if (count == 0) { print "9999"; } else { print sum / count; } sum = 0; count = 0; year++; if (year % 4 == 1) { target += 4; } else { target += 3; } }哦,请记住,非常简单的闰年计算将会失败多年,尽管不是你提到的年份。
This code works for your sample data. Of course, you will need to adjust the target values:
BEGIN { year = 0; target = 3; } $1 < 9990.00 { sum += $1; count++; } NR == target { if (count == 0) { print "9999"; } else { print sum / count; } sum = 0; count = 0; year++; if (year % 4 == 1) { target += 4; } else { target += 3; } }Oh, and keep in mind that the very simplistic leap year calculation will fail for some years, although not the years you mentioned.
更多推荐
发布评论