Quantcast
Viewing all articles
Browse latest Browse all 205399

R - Calculating mean of values in data frame column cycling intervals of values in another data frame column

Starting from a two-column data frame like the following:

value - distance (km)
10  1.2
9.5 1.7
10  4
9.5 6.5
10  7
10  7
10  7.6
8.5 11.6
9.5 11.7
8.5 14.8
9   15.2
9   15.3
8.5 17.4
8   17.8
7   21.2
4   24.6

[...]

I have to calculate several averages of the left column values while the values of the right column are between predetermined ranges of 10 km: an average of the values between 0 and 10 km, an average of the values between 5 and 15 km, 10 and 20 km and so on. Something like a moving average within predefined intervals with a partial overlap. The output should be something like the following:

9.86 (the average of 0 km <= values < 10 km)
9.43 (... 5 km <= values < 15 km)
8.71 (... 10 km <= values < 20 km)
[...]
avgX (... 40 km <= values < 50 km)

I'm looking at the documentation of the different R moving average implementations but (because of me for sure) I'm not sure of the best/right way to achieve my goal.


Viewing all articles
Browse latest Browse all 205399

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>