通过移动分组列变量的差异来计算列的平均值

Awk average of column by moving difference of grouping column variable

我有一个如下所示的文件:

1 snp1 0.0 4
1 snp2 0.2 6
1 snp3 0.3 4
1 snp4 0.4 3
1 snp5 0.5 5
1 snp6 0.6 6
1 snp7 1.3 5
1 snp8 1.3 3
1 snp9 1.9 4

文件按第 3 列排序。我希望第 4 列的平均值按第 3 列每 0.5 个单位分组。例如它应该像这样输出:

1 snp1 0.0 4.4
1 snp6 0.6 6.0
1 snp7 1.3 4.0
1 snp9 1.9 4.0

我可以像这样打印没有平均值的所有位置:

awk 'NR==1 {pos=; print [=12=]} >=pos+0.5{pos=; print [=12=]}' input

但我不知道如何打印第 4 列的平均值。如果有人可以帮助我找到解决此问题的方法,那就太好了。谢谢!

可能是这样的:

awk '
  NR==1 {c1=; c2=; v=; n=1; s=; next}
  >v+0.5 {print c1, c2, v, s/n; c1=; c2=; v=; n=1; s=; next}
  {n+=1; s+=}
  END {print c1, c2, v, s/n}
' input