Blake43
Blake43

Reputation: 11

R programming help in calculating averages at different intervals

I have a set of data that refers to my Time and my Distance as I run...so I have 2 columns which pertains to Time and Distance. Lets say I ran, 3000m overall. What I want is the average distance I travelled at 30 second intervals...hence I want the average distance I travelled from 0-30 s, 30 -60 s etc....

I did the following code:

tapply(data$Distance,cut(data$Time,pretty(range(data$Time),high.u.bias=0.1)),mean)

but this gave me the average at 200 s intervals...how do I change that?

Upvotes: 0

Views: 1455

Answers (1)

Ricardo Saporta
Ricardo Saporta

Reputation: 55350

your cut statement should probably be something like

# cutting every 30 seconds, starting at 0 
#    and going up to 30 seconds more than max of Times
cut(dat$Times, breaks=seq(0, max(dat$Times)+30, 30))
# if your time is in minutes, replace 30 with 0.5 

# Then you can assign it into your data frame if you'd like, 
#  but not necessary

cuts <- cut(dat$Times, breaks=seq(0, max(dat$Times)+30, 30))
by(dat$Dist, cuts, mean)

I'm assuming dat is your data frame and Dist is the vector you're wanting to average and Times is, well... you get the idea.

Upvotes: 1

Related Questions