How divide or add times? I have a time in a race of 10 km and I want to find the pace per km for each runner from the result of the race.
For example, if I have a data.frame
with a column time
as 00:50:00
in a row (for example) for a run of 10 km how can I find the pace for 1 km ("00:50:00" / 10 km). With strptime
I transform the string in H:M:S
but when I will divide the column by 10 to find the pace in one km, R answers Erreur dans Ops.POSIXt(d$temps, 10) : '/' non défini pour des objets "POSIXt
. And I have unexpected date in time with strptime
.
Thanks
Asked
Active
Viewed 237 times
1
-
Before doing date/time arithmetic, [you need to convert to date objets](http://stackoverflow.com/questions/12649641/calculating-time-difference-in-r). – Will Beason Sep 08 '14 at 15:28
2 Answers
2
Here is one approach.
id <- c("ana", "bob", "caroline")
time <- c("00:50:00", "00:37:00", "00:41:30")
foo <- data.frame(id, time, stringsAsFactors=F)
library(chron)
foo$time <- chron(times=foo$time)
id time
1 ana 00:50:00
2 bob 00:37:00
3 caroline 00:41:30
foo$pace <- foo$time / 10
id time pace
1 ana 00:50:00 00:05:00
2 bob 00:37:00 00:03:42
3 caroline 00:41:30 00:04:09

jazzurro
- 23,179
- 35
- 66
- 76
0
Since you didn't provide a reproducible question and we don't know what format your data is in I've mocked up the following:
time1 <- as.POSIXct(Sys.time())
time2 <- time1 + 1000 # add 1000 seconds-16 2/3 mins
time_elapsed <- time1-time2
distance <- 10
time_elap_dist <- time_elapsed/distance
# Time difference of -1.666667 mins
To do calculations with time you need your data to be in the correct format.

n8sty
- 1,418
- 1
- 14
- 26