Calculate mean of each column ignoring missing data with awk

后端 未结 2 852
死守一世寂寞
死守一世寂寞 2021-01-15 00:43

I have a large tab-separated data table with thousands of rows and dozens of columns and it has missing data marked as \"na\". For example,

na  0.93    na  0         


        
2条回答
  •  Happy的楠姐
    2021-01-15 01:31

    A possible solution:

    awk -F"\t" '{for(i=1; i <= NF; i++)
                    {if($i == $i+0){sum[i]+=$i; denom[i] += 1;}}}
                END{for(i=1; i<= NF; i++){line=line""sum[i]/(denom[i]?denom[i]:1)FS} 
                    print line}' inputFile
    

    The output for the given data:

    0.973333    0.9825  0   0.7425  0.01    0.7125
    

    Note that the third column contains only "na" and the output is 0. If you want the output to be na, then change the END{...}-block to:

    END{for(i=1; i<= NF; i++){line=line""(denom[i] ? sum[i]/denom[i]:"na")FS} print line}'

提交回复
热议问题