zoo
provides infrastructure for ordered observations
which are stored internally in a vector or matrix with an
index attribute (of arbitrary class, see below). The index
must have the same length as NROW(x)
except in the
case of a zero length numeric vector in which case the index
length can be any length. Emphasis has
been given to make all methods independent of the index/time class
(given in order.by
). In principle, the data x
could also
be arbitrary, but currently there is only support for vectors and matrices
and partial support for factors.zoo
is particularly aimed at irregular time series of numeric
vectors/matrices. zoo
's key design goals are independence of a particular
index/date/time class and consistency
with ts
and base R by providing methods to standard generics. Therefore,
standard functions can be used to work with "zoo"
objects and
memorization of new commands is reduced.
When creating a "zoo"
object with the function zoo
,
the vector of indexes order.by
can be of (a single) arbitrary class
(if x
is shorter or longer than order.by
it is
expanded accordingly),
but it is essential that ORDER(order.by)
works. For other
functions it is assumed that c()
, length()
,
MATCH()
and subsetting [,
work. If this is not the case
for a particular index/date/time class, then methods for these
generic functions should be created by the user. Note, that to achieve this,
new generic functions ORDER
and MATCH
are created in
the zoo
package with default methods corresponding to
the non-generic base functions order
and match
.
Methods to standard generics for "zoo"
objects currently
include: print
(see above), summary
, str
, head
,
tail
, [
(subsetting), rbind
, cbind
, merge
(see merge.zoo
), aggregate
(see aggregate.zoo
),
plot
and lines
(see plot.zoo
).
Additionally, zoo
provides several generic functions and methods
to work (a) on the data contained in a "zoo"
object, (b) the
index (or time) attribute associated to it, and (c) on both data and
index:
(a) The data contained in "zoo"
objects can be extracted by
coredata
(strips off all "zoo"
-specific attributes) and modified
using coredata<-
. Both are new generic functions with methods for
"zoo"
objects, see coredata
.
(b) The index associated with a "zoo"
object can be extracted
by index
and modified by index<-
. As the interpretation
of the index as ``time'' in time series applications is more natural,
there are also synonymous methods time
and time<-
. The
start and the end of the index/time vector can be queried by
start
and end
. See index
.
(c) To work on both data and index/time, zoo
provides methods
lag
, diff
(see lag.zoo
) and window
,
window<-
(see window.zoo
).
In addition to standard group generic function (see Ops
),
the following mathematical operations are availabe as methods for
"zoo"
objects: transpose t
which coerces to a matrix
first, and cumsum
, cumprod
, cummin
, cummax
which are applied column wise.
Coercion to and from "zoo"
objects is available for objects of
various classes, in particular "ts"
, "irts"
and "its"
objects can be coerced to "zoo"
, the reverse is available for
"its"
and for "irts"
(the latter in package tseries
).
Furthermore, "zoo"
objects can be coerced to vectors, matrices and
lists and data frames (dropping the index/time attribute). See as.zoo
.
Four methods are available for NA
handling in the data of
"zoo"
objects: na.omit
which returns a "zoo"
object with incomplete observations removed, na.locf
which
replaces NA
s by the last previous non-NA
, na.contiguous
which extracts the longest consecutive stretch of non-missing values
in a "zoo"
object and na.approx
which uses
linear interpolation to fill in NA
values.