window.zoo.RdMethods for extracting time windows
of "zoo" objects and replacing it.
an object.
the index/time window which should be extracted.
an index/time value. Only the indexes in index
which are greater or equal to start are used. If the index
class supports comparisons to character variables, as does "Date"
class, "yearmon" class, "yearqtr" class and
the chron package classes "dates" and "times"
then start may alternately be a character variable.
an index/time value. Only the indexes in index
which are lower or equal to end are used. Similar comments
about character variables mentioned under start apply
here too.
a suitable value object for use with window(x).
currently not used.
Either the time window of the object is extracted (and hence return a "zoo"
object) or it is replaced.
suppressWarnings(RNGversion("3.5.0"))
set.seed(1)
## zoo example
x.date <- as.Date(paste(2003, rep(1:4, 4:1), seq(1,19,2), sep = "-"))
x <- zoo(matrix(rnorm(20), ncol = 2), x.date)
x
#>
#> 2003-01-01 -0.6264538 1.51178117
#> 2003-01-03 0.1836433 0.38984324
#> 2003-01-05 -0.8356286 -0.62124058
#> 2003-01-07 1.5952808 -2.21469989
#> 2003-02-09 0.3295078 1.12493092
#> 2003-02-11 -0.8204684 -0.04493361
#> 2003-02-13 0.4874291 -0.01619026
#> 2003-03-15 0.7383247 0.94383621
#> 2003-03-17 0.5757814 0.82122120
#> 2003-04-19 -0.3053884 0.59390132
window(x, start = as.Date("2003-02-01"), end = as.Date("2003-03-01"))
#>
#> 2003-02-09 0.3295078 1.12493092
#> 2003-02-11 -0.8204684 -0.04493361
#> 2003-02-13 0.4874291 -0.01619026
window(x, index = x.date[1:6], start = as.Date("2003-02-01"))
#>
#> 2003-02-09 0.3295078 1.12493092
#> 2003-02-11 -0.8204684 -0.04493361
window(x, index = x.date[c(4, 8, 10)])
#>
#> 2003-01-07 1.5952808 -2.2146999
#> 2003-03-15 0.7383247 0.9438362
#> 2003-04-19 -0.3053884 0.5939013
window(x, index = x.date[c(4, 8, 10)]) <- matrix(1:6, ncol = 2)
x
#>
#> 2003-01-01 -0.6264538 1.51178117
#> 2003-01-03 0.1836433 0.38984324
#> 2003-01-05 -0.8356286 -0.62124058
#> 2003-01-07 1.0000000 4.00000000
#> 2003-02-09 0.3295078 1.12493092
#> 2003-02-11 -0.8204684 -0.04493361
#> 2003-02-13 0.4874291 -0.01619026
#> 2003-03-15 2.0000000 5.00000000
#> 2003-03-17 0.5757814 0.82122120
#> 2003-04-19 3.0000000 6.00000000
## for classes that support comparisons with "character" variables
## start and end may be "character".
window(x, start = "2003-02-01")
#>
#> 2003-02-09 0.3295078 1.12493092
#> 2003-02-11 -0.8204684 -0.04493361
#> 2003-02-13 0.4874291 -0.01619026
#> 2003-03-15 2.0000000 5.00000000
#> 2003-03-17 0.5757814 0.82122120
#> 2003-04-19 3.0000000 6.00000000
## zooreg example (with plain numeric index)
z <- zooreg(rnorm(10), start = 2000, freq = 4)
window(z, start = 2001.75)
#> 2001 Q4 2002 Q1 2002 Q2
#> -1.4707524 -0.4781501 0.4179416
window(z, start = c(2001, 4))
#> 2000 Q2 2000 Q4 2001 Q1 2001 Q2 2001 Q3 2001 Q4
#> 0.78213630 -1.98935170 0.61982575 -0.05612874 -0.15579551 -1.47075238
#> 2002 Q1 2002 Q2
#> -0.47815006 0.41794156
## replace data at times of d0 which are in dn
d1 <- d0 <- zoo(1:10) + 100
dn <- - head(d0, 4)
window(d1, time(dn)) <- coredata(dn)
## if the underlying time index is a float, note that the index may
## print in the same way but actually be different (e.g., differing
## by 0.1 second in this example)
zp <- zoo(1:4, as.POSIXct("2000-01-01 00:00:00") + c(-3600, 0, 0.1, 3600))
## and then the >= start and <= end may not select all intended
## observations and adding/subtracting some "fuzz" may be needed
window(zp, end = "2000-01-01 00:00:00")
#> 1999-12-31 23:00:00 2000-01-01 00:00:00
#> 1 2
window(zp, end = as.POSIXct("2000-01-01 00:00:00") + 0.5)
#> 1999-12-31 23:00:00 2000-01-01 00:00:00 2000-01-01 00:00:00
#> 1 2 3