- **Programming**
(*http://www.linuxquestions.org/questions/programming-9/*)

- - **awk syntax for calculating average and standard deviation**
(*http://www.linuxquestions.org/questions/programming-9/awk-syntax-for-calculating-average-and-standard-deviation-876246/*)

awk syntax for calculating average and standard deviationI have a data in a column.
Code:
`2` I suppose get Code:
`avg=4.2` Code:
`#!/usr/bin/awk -f` Code:
`x x-avg(x)` Many thanks in advance. |

I don't really understand the math that you want to do, but here's the math that you actually did.
Code:
(e.g. line one: "delX" = "$col-avg" = "2-2" = "0"). |

As far as I'm concerned it doesn't make too much sense to use
an average that's a moving target (changes w/ each row) to calculate anything :} |

I agree with the others that what you require mathematically is not clear, but this seems to give the results you want:
Code:
`#!/usr/bin/awk -f` |

I also think it's risky to rely on NR for the calculation, if there's a possibility of blank lines or non-numeric data occurring in the input. It would be safer to test for proper input and keep a running tab of the number of lines processed.
Code:
Edit: if there could be decimal or negative numbers, you'll have to widen the test. Something like this: Code:
`if ( $1 ~ /^-?[[:digit:]]+(\.[[:digit:]]+)?$/ )` Code:
`if ( $1 + 0.0 == $1 )` |

Thank you for the reply. Basically I want to calculate average and standard deviation from data as below:
Quote:
After that I want to calculate the average and standard deviation line by line starting from column 2 until column 5 from each line. For achieve this I tried to work out from model data for calculating average and standard deviation. I just wonder how to write awk code for doing this. thank you in advance. |

Well I had a bit of a play ... you can do what you will but this should get you going in some sort of direction :)
Code:
`#!/usr/bin/awk -f` @David - looks good, only suggestion for shorter would be to just implement the 'if' as the test to sum: Code:
`$1+0==$1{sum+= num[++n] = $1 }` |

All times are GMT -5. The time now is 01:52 PM. |