Hi,
Is there a good way to deal with time periods such as 05:30 (5 minutes, 30 seconds) in R?
Alternatively what's the fastest way to convert it into an integer with just seconds?
I can only convert to dates and can't really find a data type for time.
I'm using R with zoo.
Thanks a lot !
Seconds was the best way to deal with this. I adapted Shane's code below to my purposes, here's the result.
# time - time in the format of dd hh:mm:ss
# (That's the format used in cvs export from Alcatel CCS reports)
#
time.to.seconds <- function(time) {
t <- strsplit(as.character(time), " |:")[[1]]
seconds <- NaN
if (length(t) == 1 )
seconds <- as.numeric(t[1])
else if (length(t) == 2)
seconds <- as.numeric(t[1]) * 60 + as.numeric(t[2])
else if (length(t) == 3)
seconds <- (as.numeric(t[1]) * 60 * 60
+ as.numeric(t[2]) * 60 + as.numeric(t[3]))
else if (length(t) == 4)
seconds <- (as.numeric(t[1]) * 24 * 60 * 60 +
as.numeric(t[2]) * 60 * 60 + as.numeric(t[3]) * 60 +
as.numeric(t[4]))
return(seconds)
}