I need to print the standard deviation of measurements ($2) for each unique ID ($1).
the data looks like this:
101 560
101 460
101 530
101 480
104 600
104 510
104 500
107 450
107 490
107 550
107 500
R or datamash are probably a better choice!
Following standard deviation definition, we can:
$ cat my-sd
#!/usr/bin/awk -f
{ s[$1]["sum"] += $2 ;
n = s[$1]["oco"] ++;
v[$1][n]=$2 }
END { for(x in s){
m=s[x]["sum"]/s[x]["oco"];
s1=0;
for(y in v[x]){
s1 += (v[x][y]-m)*(v[x][y]-m);}
print x, sqrt(s1/s[x]["oco"])}
}
$ my-sd example
101 39.6074
104 44.9691
107 35.6195
datamash -Ws groupby 1 sstdev 2 < file
(change topstdev
if you want the population standard deviation) – steeldriver Jan 11 '17 at 12:59