Calculate the average of a column based on the value of another column

Hi,

I would like to calculate the average of column 'y' based on the value of column 'pos'.

For example, here is file1

id   pos    y   c
11   1      220   aa
11   4333     207   f
11   5333     112   ee
11   11116     305   e
11   11117     310   r
11   22228    781   gg
11   33310    121   hhh
11   55511    981   rr
11   111112      22    e
...

What I wanted is to calculate the average of "y" based on "pos", specifically, I want the average of y for pos from 1-10000, 10001-20000, ..., and out put would look like,

outputfile
id    pos     mean.y
11   1        179.6667                   because 179.6667=(220+207+112)/3
11   10001  307.5                                   307.5=(305+310)/2
11   20001  781                                      781=781/1
11   30001  121                                      121=121/1
11   40001  0                             because there is no 'pos' from 40001 to 5000
11   50001  981
...

Thanks a lot!

Note, the 'pos' of the outputfile is the starting value of a range.

PLEASE use code tags as demanded!
Try this as a starting point; the 40000 line I've left to your exercise...

awk 'NR==1  {print "id pos   mean.y"}
     NR>1   {tmp=($2-$2%10000)/10000;
             if (tmp!=cnt) {printf "%s %05d% 8.4f\n", ID, cnt*10000+1, sum/n; cnt=tmp; n=sum=0}
             ID=$1; sum+=$3; n++; 
            }
    ' file
id pos   mean.y
11 00001 179.6667
11 10001 307.5000
11 20001 781.0000
11 30001 121.0000
11 50001 981.0000

or

awk 'NR==1    {print "id pos   mean.y"; next}
              {tmp=($2-$2%10000)/10000}
     tmp!=cnt {printf "%s %05d% 8.4f\n", ID, cnt*10000+1, sum/n; cnt=tmp; n=sum=0}
              {ID=$1; sum+=$3; n++ }
    ' file

It works like a charm!

Thank you so much!