Emma - 9 months ago 48
R Question

# Averaging column data in R for a range of values

My dataset (testdata) is arranged like this:

Time 1 2 3
1 0 569.607 557.689 527.353
2 1 569.208 556.697 527.162
3 2 569.278 556.919 528.125
4 3 570.059 555.621 527.158

I need to find and tabulate averages for columns 1, 2 and 3 during specific ranges of time. For example, I need averages of column 1 where time is 20-30, then for 40-50, then for 60-70.

In order to find the average for 20:30, I have used:

sapply(testdata[testdata\$Time %in% c(20:30), ],mean)

which results in

Time 1 2 3
25.5000 570.5897 557.3413 528.5433

This is ok for one time range, but I'd like to calculate several at once - is there a way to do this? Thanks!