ragesz
ragesz

Reputation: 9527

R time interval data type

Is there a time interval data (variable) type in R? I have a CSV file with datetime and timeinterval columns. The data type of the datetime column can be POSIXlt, but I don't know how to set a timeinterval data type for the other column. Is it possible, or what is the best way to handle time inervals in R?

The time interval values in my CSV file looks like this [<number of days> %H:%M:%S]:

'0 20:32:59'

In Python pandas, there is a timedelta64[ns] data type for time intervals.

Thank you!

Upvotes: 0

Views: 898

Answers (2)

Richie Cotton
Richie Cotton

Reputation: 121057

Split the strings into the number of days and the time, using stringi, then use lubridate to manipulate the components.

library(stringi)
library(lubridate)

In the following example:

  • ([0-9]+) means capture one or more digits.
  • + means one or more spaces (not captured).
  • ([0-9]{2}:[0-9]{2}:[0-9]{2}) means capture 2 digits, a colon, 2 digits, another colon, and 2 more digits.

x <- "0 20:32:59" matches <- stri_match_first_regex(x, "([0-9]+) +([0-9]{2}:[0-9]{2}:[0-9]{2})")

The number of days is the second column, and the hours/minutes/seconds are in the third column.

days creates a period of the number of days; hms creates a period of hours, minutes, and seconds.

n_days <- days(as.integer(matches[, 2]))
time <- hms(matches[, 3])

Now your total is just n_days + time, though presumably you want this relative to some origin, for example:

Sys.time() + n_days + time

Upvotes: 1

bph
bph

Reputation: 23

Yes, see ? difftime

If your csv is already split into columns, apply as.difftime to one and as.POSIXlt to the other.

For example:

as.difftime(0, units="days") + as.POSIXlt("20:32:59", format="%H:%M:%S")

[Edit] If the entire result is to be an interval, this would do it:

as.difftime(0, units="days") + as.difftime("20:32:59", format="%H:%M:%S")

Upvotes: 0

Related Questions