How to work with time, distance and speed?

I study R, analyzing the results of a bike ride, and I am having problems with time data (how many people took to finish the race).

Time data is in the format "HH: MM: SS".

I tried converting it to posixct, but it adds a date component to it. I also tried the chron package, but it will not let me divide the number by a time object

One of the things I want to do is to calculate average speeds using this time, so I need to be able to split the distance by time.

+5
source share
3 answers

chron , - , times(). :

library(chron)
tms <- c("2:06:00", "3:34:30", "4:12:59")
x <- times(tms)

times, .

str(x)
Class 'times'  atomic [1:3] 0.0875 0.149 0.1757
  ..- attr(*, "format")= chr "h:m:s"

, dates as.numeric.

dist <- 42.2
as.numeric(dist/x/24)
[1] 20.09524 11.80420 10.00856

: /.

+5

POSIXct, R .

, , , , , 1 . , .. .

: as.numeric() POSIXct ( ), , , ( db datetime), . --- () , . POSIXct , , , ,...

:

R> txt <- c("08:09:10", "09:10:11", "10:11:12", "11:12:13")
R> times <- as.POSIXct(paste("2013-01-01", txt))
R> times
[1] "2013-01-01 08:09:10 CST" "2013-01-01 09:10:11 CST" 
+   "2013-01-01 10:11:12 CST" "2013-01-01 11:12:13 CST"
R> times - times[1]
Time differences in secs
[1]     0  3661  7322 10983
attr(,"tzone")
[1] ""
R> as.numeric(times - times[1])
[1]     0  3661  7322 10983
R> 
+5

, , - , . . R difftime .

tms <- c("2:06:00", "3:34:30", "4:12:59", "08:09:10",
         "09:10:11", "10:11:12", "11:12:13")

ta <- as.difftime(tms)

> ta
Time differences in hours
[1]  2.100000  3.575000  4.216389  8.152778  9.169722 10.186667 11.203611
attr(,"tzone")
[1] ""
> format(ta)
[1] " 2.100000 hours" " 3.575000 hours" " 4.216389 hours" " 8.152778 hours" " 9.169722 hours"
[6] "10.186667 hours" "11.203611 hours"

.

> 42.2/as.numeric(ta)
[1] 20.095238 11.804196 10.008564  5.176150  4.602102  4.142670  3.766643

lubridate , , duration.

library("lubridate")
ti <- as.duration(as.difftime(tms))

> ti
[1] 7560s (~2.1 hours)    12870s (~3.58 hours)  15179s (~4.22 hours)  29350s (~8.15 hours) 
[5] 33011s (~9.17 hours)  36672s (~10.19 hours) 40333s (~11.2 hours) 

and you can do the math with after converting to numeric (here, seconds, not hours)

> 42.2/as.numeric(ti)
[1] 0.005582011 0.003278943 0.002780157 0.001437819 0.001278362 0.001150742 0.001046290
+3
source

All Articles