0

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?

Blake43
  • 11
  • 1
  • 6
  • Show us your data (or better use `dput` to paste your data) to work with please. – Arun Feb 19 '13 at 06:37
  • Cute.. you should edit your previous question. http://stackoverflow.com/questions/14949819/r-programming-seperating-data-into-bins-and-calculating-averages – N8TRO Feb 19 '13 at 06:53
  • 2
    @NathanG Aw man, and I worked hard on that answer too... – sebastian-c Feb 19 '13 at 07:03
  • @Blake43 If I had to guess, I'd guess you should replace `pretty(range(data$Time),high.u.bias=0.1)` with `seq(0, max(data$time), 200)`. Try using something like `cut(data$Time, c(seq(0, max(data$Time), 30), Inf))` in the place of your `cut`. – sebastian-c Feb 19 '13 at 07:09
  • @sebastian-c Sorry I thought it was the same question at first.. And an impressive answer you gave too. – N8TRO Feb 19 '13 at 07:16

1 Answers1

1

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.

Ricardo Saporta
  • 54,400
  • 17
  • 144
  • 178
  • Thanks for this ricardo...I was wondering is it possible to plot these data...like the breaks Vs the cuts (which are the averages) ?? – Blake43 Feb 19 '13 at 10:00
  • you can plot anything you'd like ;) You might want to post that as its own question though – Ricardo Saporta Feb 19 '13 at 22:03