我有时间序列
mainTimeSeries <- data.frame(time=seq(as.POSIXct("2012/1/1"), as.POSIXct("2012/1/5"), "hour"), value=sample(1:10, 1))
我想过滤掉另一个系列中存在的所有数据
badTimeSeries<-data.frame(startTime=seq(as.POSIXct("2012/1/3"), as.POSIXct("2012/1/4"), "hour"))
badTimeSeries$endTime <- badTimeSeries$startTime + 1800
是否存在过滤日期的现有功能?结果应该是mainTimeSeries的元素不应该在badTimeSeries的startTime和endTime之间。
答案 0 :(得分:6)
lubridate
在这里很有用。如果没有它,你需要编写自己的重叠检查,这有点痛苦......
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
>
或者:
> 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"
>
仅使用基础R:
bad_start <- as.POSIXct('2012/1/3')
bad_end <- as.POSIXct('2012/1/4')
mainTimeSeries[mainTimeSeries > bad_end | mainTimeSeries < bad_start]