【发布时间】:2017-09-29 14:38:02
【问题描述】:
我有这个数据框
r2 distance
1 33.64 67866
2 8.50 77229
3 15.07 109119
4 24.35 142279
5 7.74 143393
6 8.21 177670
7 12.26 216440
8 12.66 253751
9 26.31 282556
10 39.08 320816
我需要为distance 列中两个值之间的距离等于或小于100000 的每个行块计算列r2 的平均值。
对于此示例,所需的输出将是:
mean_r2 diff_of_distance
1 17.86 75527 ## mean of rows 1 to 5; distance 5 - distance 1
2 13.91 66164 ## mean of rows 2 to 5; distance 5 - distance 2
3 13.84 68551 ## mean of rows 3 to 6; distance 6 - distance 3
4 13.14 74161 ## mean of rows 4 to 7; distance 7 - distance 4
5 9.40 73047 ## mean of rows 5 to 7; distance 7 - distance 5
6 11.04 76081 ## mean of rows 6 to 8; distance 8 - distance 6
等等。
编辑 1:我有超过 100,000 行。
谢谢。
【问题讨论】:
-
您的问题令人困惑,因为您显示了第 2-5 行的平均值,但是 77,229(第 2 行)不在 109119(第 3 行)的 10,000 之内。
-
@AdrianMartin 你说得对,我的意思是 100,000。编辑了问题。
标签: r loops dataframe aggregate apply