Examples for 'zoo::index'


Extracting/Replacing the Index of Objects

Aliases: index index.default index.zoo index.ts time.zoo index<- index<-.zoo time<- time<-.zoo start.zoo end.zoo

Keywords: ts

### ** Examples

suppressWarnings(RNGversion("3.5.0"))
set.seed(1)

x.date <- as.Date(paste(2003, 2, c(1, 3, 7, 9, 14), sep = "-"))
x <- zoo(rnorm(5), x.date)

## query index/time of a zoo object
index(x)
[1] "2003-02-01" "2003-02-03" "2003-02-07" "2003-02-09" "2003-02-14"
time(x)
[1] "2003-02-01" "2003-02-03" "2003-02-07" "2003-02-09" "2003-02-14"
## change class of index from Date to POSIXct
## relative to current time zone
x
2003-02-01 2003-02-03 2003-02-07 2003-02-09 2003-02-14 
-0.6264538  0.1836433 -0.8356286  1.5952808  0.3295078 
index(x) <- as.POSIXct(format(time(x)),tz="")
x
2003-02-01 2003-02-03 2003-02-07 2003-02-09 2003-02-14 
-0.6264538  0.1836433 -0.8356286  1.5952808  0.3295078 
## replace index/time of a zoo object
index(x) <- 1:5
x
         1          2          3          4          5 
-0.6264538  0.1836433 -0.8356286  1.5952808  0.3295078 
time(x) <- 6:10
x
         6          7          8          9         10 
-0.6264538  0.1836433 -0.8356286  1.5952808  0.3295078 
## query start and end of a zoo object
start(x)
[1] 6
end(x)
[1] 10
## query index of a usual matrix
xm <- matrix(rnorm(10), ncol = 2)
index(xm)
[1] 1 2 3 4 5

[Package zoo version 1.8-10 Index]