Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting average per line

Tags:

awk

average

I have a large data set in this format

HF TLLL A T 0.999 NA 0.666 NA 0.566 NA NA 0.87
HF TLLM A T 0.500 0.500 0.666 0.566 NA NA 0.87

I want to calculate an average for each line, starting from column 5 until end of line, and ignoring the string NA. Then append the average to the end of each line.

The output would look like this:

HF TLLL A T 0.999 NA 0.666 NA 0.566 NA NA 0.87 0.775
HF TLLM A T 0.500 0.500 0.666 0.566 NA NA 0.87 0.620

I have been getting the sum like this, but can't figure out how to keep track of the number of integers that were summed, in order to calculate the average.

awk '{x=0;for(i=5;i<=NF;i++)x=x+$i;print $0, x}'
like image 347
user1308144 Avatar asked Nov 14 '13 17:11

user1308144


3 Answers

$ cat file
HF TLLL A T 0.999 NA 0.666 NA 0.566 NA NA 0.87
HF TLLM A T 0.500 0.500 0.666 0.566 NA NA 0.87
HF TLLM A T NA NA NA NA NA NA NA

$ awk '{sum=cnt=0; for (i=5;i<=NF;i++) if ($i != "NA") { sum+=$i; cnt++ } print $0, (cnt ? sum/cnt : "NA") }' file
HF TLLL A T 0.999 NA 0.666 NA 0.566 NA NA 0.87 0.77525
HF TLLM A T 0.500 0.500 0.666 0.566 NA NA 0.87 0.6204
HF TLLM A T NA NA NA NA NA NA NA NA

The ternary expression avoids a divide by zero error on input row 3 where every data field is "NA".

like image 150
Ed Morton Avatar answered Oct 21 '22 07:10

Ed Morton


kent$  awk '{s=n=0;for(i=5;i<=NF;i++)if($i!="NA"){s+=$i*1;n++}printf "%s %.3f\n",$0,s/n}' file
HF TLLL A T 0.999 NA 0.666 NA 0.566 NA NA 0.87 0.775
HF TLLM A T 0.500 0.500 0.666 0.566 NA NA 0.87 0.620
like image 21
Kent Avatar answered Oct 21 '22 08:10

Kent


Using awk, you can do this:

awk '{for (i=5;i<=NF;i++) {if ($i!="NA") t++;a+=$i}print $0,a/t;a=t=0}' file
HF TLLL A T 0.999 NA 0.666 NA 0.566 NA NA 0.87 0.77525
HF TLLM A T 0.500 0.500 0.666 0.566 NA NA 0.87 0.6204
like image 45
Jotne Avatar answered Oct 21 '22 06:10

Jotne