Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Output mean and standard deviation of a row

Shell Programming and Scripting


Tags
mean std awk

Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-26-2013
kayak kayak is offline
Registered User
 
Join Date: Aug 2012
Last Activity: 26 October 2013, 7:02 AM EDT
Posts: 29
Thanks: 11
Thanked 1 Time in 1 Post
Output mean and standard deviation of a row

I have a file that looks that this:


Code:
820 890 530
1650 1600 1800
1850 1900 2270
1640 2300 1670
2080 2200 2350
1150 1630 2210

I would like to output the mean and standard deviation of each row so that my final output would look like this


Code:
820 890 530 746.667 155.849
1650 1600 1800 1683.33 84.9837
1850 1900 2270 2006.67 187.32
1640 2300 1670 1870 304.302
2080 2200 2350 2210 110.454
1150 1630 2210 1663.33 433.385

mean is calculated as average and standard dev is:


Code:
sqrt((sum((x-mean)**2))/N)

In my case N=3. Can someone help with awk

Last edited by kayak; 03-26-2013 at 10:39 AM..
Sponsored Links
    #2  
Old Unix and Linux 03-26-2013
PikK45 PikK45 is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 26 November 2014, 5:10 AM EST
Location: Chennai
Posts: 567
Thanks: 43
Thanked 74 Times in 72 Posts
Calculating mean is easier Linux


Code:
awk '{sum=0; for (i=1; i<=NF; i++) {sum=sum+$i;} m=sum/NF; print $0, m; }' file

Sponsored Links
    #3  
Old Unix and Linux 03-26-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 6 July 2015, 7:43 PM EDT
Location: Galactic Empire
Posts: 3,409
Thanks: 240
Thanked 1,217 Times in 1,143 Posts

Code:
awk ' BEGIN {
        N = 3
} {
        for ( i = 1; i <= NF; i++ )
        {
                rec[NR] = $0
                sum[NR] += $i
                sumsq[NR] += $i * $i
        }
} END {
        for ( i = 1; i <= NR; i++ )
                print rec[i]" "sum[i]/N" "sqrt(sumsq[i]/N - (sum[i]/N)**2)
} ' file

    #4  
Old Unix and Linux 03-26-2013
PikK45 PikK45 is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 26 November 2014, 5:10 AM EST
Location: Chennai
Posts: 567
Thanks: 43
Thanked 74 Times in 72 Posts
Quote:
Originally Posted by Yoda View Post
print rec[i]" "sum[i]/N" "sqrt(sumsq[i]/N - (sum[i]/N)**2)
[/CODE]
Why can't we use NF instead of N here??
Sponsored Links
    #5  
Old Unix and Linux 03-26-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 6 July 2015, 7:43 PM EDT
Location: Galactic Empire
Posts: 3,409
Thanks: 240
Thanked 1,217 Times in 1,143 Posts
Quote:
Originally Posted by PikK45 View Post
Why can't we use NF instead of N here??
Yes, we can use NF or 3. Does that really matter? Linux

I defined & used variable N because the requester specified it in his formula.
Sponsored Links
    #6  
Old Unix and Linux 03-26-2013
PikK45 PikK45 is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 26 November 2014, 5:10 AM EST
Location: Chennai
Posts: 567
Thanks: 43
Thanked 74 Times in 72 Posts
I just wanted to use NF so that we can use the same logic to many columns Linux

And, your awk codes work perfect Linux Started learning from you Linux
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
calculating row-wise standard deviation using awk ida1215 Shell Programming and Scripting 2 07-03-2012 02:05 PM
AWK script for standard deviation / root mean square deviation chrisjorg Shell Programming and Scripting 12 01-18-2012 11:30 AM
Standard deviation in awk gd9629 Shell Programming and Scripting 11 08-24-2011 12:40 PM
Calculating the Standard Deviation for a column kylle345 UNIX for Dummies Questions & Answers 1 05-18-2009 04:58 PM
Mean and Standard deviation lakshmikanth.pg Shell Programming and Scripting 4 04-27-2009 03:04 PM



All times are GMT -4. The time now is 08:06 PM.