Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - Filtering time series

Tags:

r

time-series

I have a time series

mainTimeSeries <- data.frame(time=seq(as.POSIXct("2012/1/1"), as.POSIXct("2012/1/5"), "hour"), value=sample(1:10, 1))

I want to filter out all the data which is present in another series

badTimeSeries<-data.frame(startTime=seq(as.POSIXct("2012/1/3"), as.POSIXct("2012/1/4"), "hour"))
badTimeSeries$endTime <- badTimeSeries$startTime + 1800

Is there an existing function to filter out dates ? The results should be such that no element of mainTimeSeries should be between startTime and endTime of badTimeSeries.

like image 255
2sb Avatar asked Aug 21 '12 16:08

2sb


1 Answers

lubridate is useful here. Without it you need to write your own checks for overlaps which is sorta a pain...

library(lubridate)

badRange <- as.interval(days(1), as.POSIXct("2012/1/3"))

> mainTimeSeries %within% badRange
 [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[22] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[43] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
[64]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[85] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> 

Or:

> mainTimeSeries[mainTimeSeries %within% badRange]
 [1] "2012-01-03 00:00:00 PST" "2012-01-03 01:00:00 PST" "2012-01-03 02:00:00 PST" "2012-01-03 03:00:00 PST"
 [5] "2012-01-03 04:00:00 PST" "2012-01-03 05:00:00 PST" "2012-01-03 06:00:00 PST" "2012-01-03 07:00:00 PST"
 [9] "2012-01-03 08:00:00 PST" "2012-01-03 09:00:00 PST" "2012-01-03 10:00:00 PST" "2012-01-03 11:00:00 PST"
[13] "2012-01-03 12:00:00 PST" "2012-01-03 13:00:00 PST" "2012-01-03 14:00:00 PST" "2012-01-03 15:00:00 PST"
[17] "2012-01-03 16:00:00 PST" "2012-01-03 17:00:00 PST" "2012-01-03 18:00:00 PST" "2012-01-03 19:00:00 PST"
[21] "2012-01-03 20:00:00 PST" "2012-01-03 21:00:00 PST" "2012-01-03 22:00:00 PST" "2012-01-03 23:00:00 PST"
[25] "2012-01-04 00:00:00 PST"
> 

Using only base R:

bad_start <- as.POSIXct('2012/1/3')
bad_end   <- as.POSIXct('2012/1/4')
mainTimeSeries[mainTimeSeries > bad_end | mainTimeSeries < bad_start]
like image 96
Justin Avatar answered Oct 13 '22 02:10

Justin