Reputation: 2867
I have an xts object which represents a variable measured over a two week time period using 15 minute sampling.
An example of the data is as follows:
> class(wideRawXTS)
[1] "xts" "zoo"
> head(wideRawXTS[,2],10)
DO0182U09B3
2017-01-20 16:30:00 -103.37
2017-01-20 16:45:00 -102.75
2017-01-20 17:00:00 -103.30
2017-01-20 17:15:00 -95.92
2017-01-20 17:30:00 -103.04
2017-01-20 17:45:00 -103.67
2017-01-20 18:00:00 -103.26
2017-01-20 18:15:00 -103.86
2017-01-20 18:30:00 -103.96
2017-01-20 18:45:00 -103.33
> str(wideRawXTS)
An ‘xts’ object on 2017-01-20 16:30:00/2017-02-03 16:00:00 containing:
Data: num [1:1343, 1:12] -102 -101 -101 -101 -101 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:12] "DO0182U09A3" "DO0182U09B3" "DO0182U09C3" "DO0182U21A1" ...
Indexed by objects of class: [POSIXlt,POSIXt] TZ:
xts Attributes:
NULL
I wish to extract each day i.e. 24 hrs of data - 2017-01-20 16:30:00 -> 2017-01-21 16:30:00 for a single variable so that I can calculate the intra day correlation for this single variable.
I've checked out Joshua Ulrich's XTS FAQ but it doesn't address data which has a date and time component.
How can I subset this XTS object into 14 day blocks (96 samples)?
Upvotes: 0
Views: 214
Reputation: 641
If you're looking for an intra-day correlation with another series, you could use xts::apply.daily
:
library(xts)
times <- seq(
from = as.POSIXct("2000-01-01 00:00:00"),
to = as.POSIXct("2000-01-10 00:00:00"),
by = 15 * 60
)
values1 <- runif(length(times))
values2 <- runif(length(times))
series <- xts(x = cbind(values1, values2), order.by = times)
apply.daily(series, FUN = function(x) cor(x = x[, 1],
y = x[, 2]))
#> [,1]
#> 2000-01-01 23:45:00 -0.067156934
#> 2000-01-02 23:45:00 -0.005426825
#> 2000-01-03 23:45:00 0.011104314
#> 2000-01-04 23:45:00 -0.085361525
#> 2000-01-05 23:45:00 -0.047193367
#> 2000-01-06 23:45:00 0.149419147
#> 2000-01-07 23:45:00 0.068223067
#> 2000-01-08 23:45:00 0.002006084
#> 2000-01-09 23:45:00 0.023573252
#> 2000-01-10 00:00:00 NA
Source: derived from Joshua Ulrich's answer here.
Upvotes: 1