index: Extracting/Replacing the Index of Objects

Description Usage Arguments Details See Also Examples

View source: R/index.R

Description

Generic functions for extracting the index of an object and replacing it.

Usage

1
2
index(x, ...)
index(x) <- value

Arguments

x

an object.

...

further arguments passed to methods.

value

an ordered vector of the same length as the "index" attribute of x.

Details

index is a generic function for extracting the index of objects, currently it has a default method and a method for zoo objects which is the same as the time method for zoo objects. Another pair of generic functions provides replacing the index or time attribute. Methods are available for "zoo" objects only, see examples below.

The start and end of the index/time can be queried by using the methods of start and end.

See Also

time, zoo

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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)
time(x)

## change class of index from Date to POSIXct
## relative to current time zone
x
index(x) <- as.POSIXct(format(time(x)),tz="")
x

## replace index/time of a zoo object
index(x) <- 1:5
x
time(x) <- 6:10
x

## query start and end of a zoo object
start(x)
end(x)

## query index of a usual matrix
xm <- matrix(rnorm(10), ncol = 2)
index(xm)

Example output

Attaching package: 'zoo'

The following objects are masked from 'package:base':

    as.Date, as.Date.numeric

[1] "2003-02-01" "2003-02-03" "2003-02-07" "2003-02-09" "2003-02-14"
[1] "2003-02-01" "2003-02-03" "2003-02-07" "2003-02-09" "2003-02-14"
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 
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 
         1          2          3          4          5 
-0.6264538  0.1836433 -0.8356286  1.5952808  0.3295078 
         6          7          8          9         10 
-0.6264538  0.1836433 -0.8356286  1.5952808  0.3295078 
[1] 6
[1] 10
[1] 1 2 3 4 5

zoo documentation built on March 9, 2021, 5:07 p.m.