How do I calculate the mean of a column

后端 未结 5 1094
花落未央
花落未央 2020-12-25 10:42

Anyone know how can I calculate the mean of one these columns (on linux)??

sda               2.91    20.44    6.13    2.95   217.53   186.67    44.55     0.         


        
相关标签:
5条回答
  • 2020-12-25 11:20

    Awk:

    awk '{ total += $2 } END { print total/NR }' yourFile.whatever
    

    Read as:

    • For each line, add column 2 to a variable 'total'.
    • At the end of the file, print 'total' divided by the number of records.
    0 讨论(0)
  • 2020-12-25 11:21

    Perl solution:

    perl -lane '$total += $F[1]; END{print $total/$.}' file
    

    -a autosplits the line into the @F array, which is indexed starting at 0
    $. is the line number

    If your fields are separated by commas instead of whitespace:

    perl -F, -lane '$total += $F[1]; END{print $total/$.}' file
    

    To print mean values of all columns, assign totals to array @t:

    perl -lane 'for $c (0..$#F){$t[$c] += $F[$c]}; END{for $c (0..$#t){print $t[$c]/$.}}' 
    

    output:

    0
    0.485
    14.38
    1.74
    0.888333333333333
    77.27
    49.8266666666667
    39.91
    1.29833333333333
    434.131666666667
    
    0 讨论(0)
  • 2020-12-25 11:28

    David Zaslavsky for the fun of it:

    with open("mean.txt", 'r') as f: 
        n,t = map(sum, zip(*((1, float(line.split()[1])) for line in f)))
    print t/n
    
    0 讨论(0)
  • 2020-12-25 11:32

    You can use python for that, is available in Linux.

    If that comes from a file, take a look at this question, just use float instead.

    For instance:

    #mean.py 
    def main():
        with open("mean.txt", 'r') as f:
            data = [map(float, line.split()) for line in f]
    
        columnTwo = []
        for row in data:
            columnTwo.append( row[1] )
    
        print  sum(columnTwo,0.0) / len( columnTwo )
    
    
    
    if __name__=="__main__":
        main()
    

    Prints 14.38

    I just include the data in the mean.txt file, not the row header: "sda"

    0 讨论(0)
  • 2020-12-25 11:35

    Simple-r will calculate the mean with the following line:

    r -k2 mean file.txt
    

    for the second column. It can also do much more sophisticated statistical analysis, since it uses R environment for all of its statistical analysis.

    0 讨论(0)
提交回复
热议问题