Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

binning of data

Status
Not open for further replies.

rkdash

Technical User
Jul 15, 2004
14
US
Hello,

I have a ascii table that looks like this:

84.575 1.775
84.593 1.773
84.618 1.768
84.642 1.768
84.666 1.763
84.685 1.761
84.696 1.759

Th first column ranges from 0 to 120 and the second column from 0 to 4. I want to "bin" the first column (within 2 units) and calculate the average of the data from the second column that fall within the "bin".

For example, I want the bins (from first column) to be like [0-2], [2-4], [4-6]..., [118-120] with bin centers 1, 3, 5, ..., 119. And average of the data from second column falling under the bins.

So, my table should be like this:

1 ?
3 ?
5 ?
....
....
119 ?



Thanks
 
Try this:
Code:
awk '{
 tot[int($1/2)+1]+=$2
 num[int($1/2)+1]++
 }
END {
 for (i=1;i<=119;i++)
  print i, num[i] ? tot[i]/num[i] : 0
}' /path/to/your/file


HTH,

p5wizard
 
p5wizard,
your 'binning' is not exactly correct. Here's something better [I hope].
nawk -f rk.awk myFile

rk.awk:
Code:
{
 bin= (($1%2) < 1) ? int($1) + 1 : int($1)
 tot[bin]+=$2
 num[bin]++
 }
END {
 for (i=1;i<=119;i+=2)
  print i, num[i] ? tot[i]/num[i] : 0
}

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top