user3650690
user3650690

Reputation: 13

Excluding hours and days in xts

I have an xts object with NA data from 1.1.2007 to 5.6.2014 with minute intervals. I need to exclude between friday 17:00 and sunday 17:00. I am aware of the tricks like ['T17:00:00/T17:00:00'] to subset but how do you work the day of week condition to exclude a range into it? So, in this,

dt.seq <- seq(c(ISOdate(2007,01,01)),c(ISOdate(2014,05,06)),by="min")
dt.NA <- xts(rep(NA,length(dt.seq)),dt.seq)

I do not want friday to sunday between 17:00 to 17:00

Thanks

Upvotes: 1

Views: 367

Answers (1)

Joshua Ulrich
Joshua Ulrich

Reputation: 176688

Here's the first thing I thought of; there may be a better way.

require(xts)
dt.seq <- seq(c(ISOdate(2007,01,01)),c(ISOdate(2014,05,06)),by="min")
dt.NA <- xts(rep(NA,length(dt.seq)),dt.seq)

wday <- .indexwday(dt.NA)       # weekday for each observation
hour <- .indexhour(dt.NA)       # hour for each observation
week.subset <-
  !((wday == 5 & hour >= 17) |  # Friday, after 17:00
    (wday == 6) |               # Saturday, all day
    (wday == 0 & hour < 17))    # Sunday, before 17:00

# Now subset your xts object
x <- dt.NA[week.subset,]
# Verify it only has the observations you want
table(.indexhour(x), .indexwday(x))

Upvotes: 2

Related Questions