Reputation: 2949
I have time-series data of three days, formatted as xts
object. The dummy data is generated as:
library(xts)
Sys.setenv(TZ="Asia/Kolkata")
seq <- timeBasedSeq('2015-06-01/2015-06-03 23')
z <- xts(1:length(seq),seq)
Now, using the index of any third day observation, I want to extract previous day observation at the same time instant. Accordingly, I coded it as
a <- z[50] # Any observation of third day will suffice
b <- as.Date(index(a), tz ="Asia/Kolkata")-1 #previous day date
z[.indexDate(z) %in% b & .indexhour(z) %in% .indexhour(a)]
The last command results in
[,1]
2015-06-03 01:00:00 50
But, It should result into
[,1]
2015-06-02 01:00:00 26
Can anyone point where I am doing wrong?
Upvotes: 1
Views: 299
Reputation: 176648
The problem is that .indexDate
is defined as .index(x) %/% 86400L
, and .index
returns the number of seconds since the origin in UTC. As you did when you constructed b
, you need to account for the timezone when converting to Date:
d <- as.Date(index(z), tz ="Asia/Kolkata")
z[d %in% b & .indexhour(z) %in% .indexhour(a)]
# [,1]
# 2015-06-02 01:00:00 26
Upvotes: 1