-1

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
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.