tovare
tovare

Reputation: 4087

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

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.

Upvotes: 4

Views: 638

Answers (2)

Peter M
Peter M

Reputation: 844

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?

Upvotes: 1

Godeke
Godeke

Reputation: 16281

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.

Upvotes: 3

Related Questions