Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operating with time intervals like 08:00-08:15

Tags:

r

time-series

I would like to import a time-series where the first field indicates a period:

08:00-08:15
08:15-08:30
08:30-08:45

Does R have any features to do this neatly?

Thanks!


Update:

The most promising solution I found, as suggested by Godeke was the cron package and using substring() to extract the start of the interval.

I'm still working on related issues, so I'll update with the solution when I get there.

like image 841
tovare Avatar asked Oct 21 '25 06:10

tovare


2 Answers

CRAN shows a package that is actively updated called "chron" that handles dates. You might want to check that and some of the other modules found here: http://cran.r-project.org/web/views/TimeSeries.html

xts and zoo handle irregular time series data on top of that. I'm not familiar with these packages, but a quick look over indicates you should be able to use them fairly easily by splitting on the hyphen and loading into the structures they provide.

like image 193
Godeke Avatar answered Oct 23 '25 22:10

Godeke


So you're given a character vector like c("08:00-08:15",08:15-08:30) and you want to convert to an internal R data type for consistency? Check out the help files for POSIXt and strftime.
How about a function like this:

importTimes <- function(t){
    t <- strsplit(t,"-")
    return(lapply(t,strptime,format="%H:%M:%S"))
}

This will take a character vector like you described, and return a list of the same length, each element of which is a POSIXt 2-vector giving the start and end times (on today's date). If you want you could add a paste("1970-01-01",x) somewhere inside the function to standardize the date you're looking at if it's an issue.

Does that help at all?

like image 45
Peter M Avatar answered Oct 23 '25 21:10

Peter M