多个文件一般不考虑不同类型的缺失值 [英] Average of multiple files without considering different kind of missing values

查看:90
本文介绍了多个文件一般不考虑不同类型的缺失值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要计算的15个文件,平均: - ifile1.txt,ifile2.txt,.....,ifile15.txt。列和每个文件的行数是一样的,但具有不同类型的缺失值(例如,-9999&放大器; 8888)。部分数据看起来

  ifile1.txt ifile2.txt ifile3.txt
 2 8888? ? 。 1 2 1 3。 5? ? ? 。
 1 -9999 8888? 。 1 8888 8888 8888。 5? ? ? 。
 4 6 5 2。 2 5 5 1。 3 4 3 1。
 5 5 7 1。 0 0 1 1。 4 3 4 0。
 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。

我想找到一个新的文件,它会显示这15费尔的平均不考虑遗漏值。

  ofile.txt
 2.66 2 1 3。 (即2 1 5平均,平均的8888 2?等等)
 2.33 -9999 -9999 -9999。
 3 5 4.33 1.33。
 3 2.66 0.66 4。
 。 。 。 。 。

这个问题是类似于我先前的问题,<一个href=\"http://stackoverflow.com/questions/33255680/average-of-multiple-files-without-considering-missing-values\">Average多个文件而不考虑遗漏值

我用以下尝试,但没有得到所期望的结果。

 的awk'
   {
     对于(i = 1; I&LT; = NF;我++){
     总和[FNR,我] + = $ I
     算上[FNR,我] + = $ I =!|?-9999 | 8888
   }
     }
  结束 {
       对于(i = 1; I&LT; = FNR;我++){
       为(J = 1; J&LT; = NF; J ++)printf的%S,计数[I,J] = 0!?总和[I,J] /计数[I,J]:|?-9999 | 8888
       打印-9999
      }
     }
   IFILE *


解决方案

 的awk'
{
    对于(i = 1; I&LT; = NF;我++){
        如果($ I〜/ ^(![?] | -9999 | 8888)$ /){
            算上[FNR,我] ++
            总和[FNR,我] + = $ I
        }
  }
}
结束 {
   对于(i = 1; I&LT; = FNR;我++){
       为(J = 1; J&LT; = NF; J ++)
           printf的%12.2f,计数[I,J]!= 0?总和[I,J] /计数[I,J]:-9999
       打印
   }
}
IFILE * .TXT

这会产生:

  2.67 2.00 1.00 3.00
    2.33 -9999.00 -9999.00 -9999.00
    3.00 5.00 4.33 1.33
    3.00 2.67 4.00 0.67

I want to calculate the average of 15 files:- ifile1.txt, ifile2.txt, ....., ifile15.txt. Number of columns and rows of each file are same, but having different type of missing values (for instance ?, -9999 & 8888). Part of the data looks as

 ifile1.txt             ifile2.txt               ifile3.txt
 2  8888   ?     ? .    1  2     1     3    .    5  ?  ?  ? .
 1  -9999  8888  ? .    1  8888  8888  8888 .    5  ?  ?  ? .
 4  6      5     2 .    2  5     5     1    .    3  4  3  1 .
 5  5      7     1 .    0  0     1     1    .    4  3  4  0 .
 .  .      .     . .    .  .     .     .    .    .  .  .  . .  

I would like to find a new file which will show the average of these 15 fils without considering the missing values.

 ofile.txt
 2.66     2        1         3      . (i.e. average of 2 1 5, average of 8888 2 ? and so on)
 2.33     -9999    -9999    -9999   .
 3        5        4.33      1.33   .
 3        2.66     4         0.66   .
 .      .     .    .      .

This question is similar to my earlier question Average of multiple files without considering missing values

I was trying with the following, but not getting the desired result.

awk '
   {
     for (i = 1;i <= NF;i++) {
     Sum[FNR,i]+=$i
     Count[FNR,i]+=$i!="?|-9999|8888"
   }
     }
  END {
       for( i = 1; i <= FNR; i++){
       for( j = 1; j <= NF; j++) printf "%s ", Count[i,j] != 0 ? Sum[i,j]/Count[i,j] : "?|-9999|8888"
       print "-9999"
      }
     }
   ' ifile*

解决方案

awk '
{
    for (i=1; i<=NF; i++) {
        if ($i !~ /^([?]|-9999|8888)$/) {
            Count[FNR,i]++
            Sum[FNR,i]+=$i
        }
  }
}
END {
   for (i=1; i<=FNR;i++){
       for (j=1; j<=NF; j++)
           printf "%12.2f ", Count[i,j]!=0 ? Sum[i,j]/Count[i,j] : -9999
       print ""
   }
}
' ifile*.txt

This produces:

    2.67         2.00         1.00         3.00 
    2.33     -9999.00     -9999.00     -9999.00 
    3.00         5.00         4.33         1.33 
    3.00         2.67         4.00         0.67 

这篇关于多个文件一般不考虑不同类型的缺失值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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