zoo function - RDocumentation (2024)

zoo provides infrastructure for ordered observationswhich are stored internally in a vector or matrix with anindex attribute (of arbitrary class, see below). The indexmust have the same length as NROW(x) except in the case of a zero length numeric vector in which case the indexlength can be any length. Emphasis hasbeen given to make all methods independent of the index/time class(given in order.by). In principle, the data x could alsobe arbitrary, but currently there is only support for vectors and matricesand partial support for factors.

zoo is particularly aimed at irregular time series of numericvectors/matrices, but it also supports regular time series (i.e.,series with a certain frequency).zoo's key design goals are independence of a particularindex/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 isexpanded accordingly),but it is essential that ORDER(order.by) works. For otherfunctions it is assumed that c(), length(),MATCH() and subsetting [, work. If this is not the casefor 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 inthe zoo package with default methods corresponding tothe non-generic base functions order and match. Note that the order and hence the default ORDER typically work if there is axtfrm method. Furthermore, for certain (but not for all)operations the index class should have an as.numeric method (in particular for regular series) and an as.character method might improveprinted output (see also below).

The index observations order.by should typically be unique, such thatthe observations can be totally ordered. Nevertheless, zoo() is able to create"zoo" objects with duplicated indexes (with a warning) and simple methods such as plot()or summary() will typically work for such objects. However, this isnot formally supported as the bulk of functionality provided in zoo requiresunique index observations/time stamps. See below for an example how to removeduplicated indexes.

If a frequency is specified when creating a series via zoo, theobject returned is actually of class "zooreg" which inherits from "zoo".This is a subclass of "zoo" which relies on having a "zoo" serieswith an additional "frequency" attribute (which has to comply with theindex of that series). Regular "zooreg" series can also be created byzooreg, the zoo analogue of ts. See the respective help page and is.regular for further details.

Methods to standard generics for "zoo" objects currentlyinclude: print (see above), summary, str, head,tail, [ (subsetting), rbind, cbind, merge(see merge.zoo), aggregate (see aggregate.zoo), rev, split (see aggregate.zoo), barplot,plot and lines (see plot.zoo). For multivariate"zoo" series with column names the $ extractor is available,behaving similar as for "data.frame" objects. Methods are alsoavailable for median and quantile.

ifelse.zoo is not a method (because ifelse is not a generic) but must be written out including the .zoo suffix.

To “prettify” printed output of "zoo" series the generic function index2char is used for turning index values into charactervalues. It defaults to using as.character but can be customizedif a different printed display should be used (although this should notbe necessary, usually).

The subsetting method [ work essentially like thecorresponding functions for vectors or matrices respectively, i.e., takesindexes of type "numeric", "integer" or "logical". Butadditionally, it can be used to index with observations from the index class ofthe series. If the index class of the series is one of the three classes above,the corresponding index has to be encapsulated in I() to enforce usage ofthe index class (see examples). Subscripting by a zoo object whosedata contains logical values is undefined.

Additionally, zoo provides several generic functions and methodsto work (a) on the data contained in a "zoo" object, (b) theindex (or time) attribute associated to it, and (c) on both data andindex:

(a) The data contained in "zoo" objects can be extracted bycoredata (strips off all "zoo"-specific attributes) and modifiedusing coredata<-. Both are new generic functions with methods for"zoo" objects, see coredata.

(b) The index associated with a "zoo" object can be extractedby index and modified by index<-. As the interpretationof the index as “time” in time series applications is more natural,there are also synonymous methods time and time<-. Thestart and the end of the index/time vector can be queried bystart and end. See index.

(c) To work on both data and index/time, zoo provides methodslag, diff (see lag.zoo) and window, window<- (see window.zoo).

In addition to standard group generic function (see Ops),the following mathematical operations are available as methods for"zoo" objects: transpose t which coerces to a matrix first, and c*msum, cumprod, cummin, cummaxwhich are applied column wise.

Coercion to and from "zoo" objects is available for objects ofvarious 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 andlists and data frames (dropping the index/time attribute). See as.zoo.

Several methods are available for NA handling in the data of "zoo" objects: na.aggregate which uses group means to fill in NAvalues,na.approx which uses linear interpolation to fill in NA values. na.contiguous which extracts the longest consecutive stretch of non-missing values in a "zoo" object,na.fill which uses fixed specified values to replace NAvalues,na.locf whichreplaces NAs by the last previous non-NA, na.omit which returns a "zoo"object with incomplete observations removed,na.spline which uses spline interpolation to fill in NA values andna.StructTS which uses a seasonal Kalman filter to fill inNA values,na.trim which trims runs of NAs off the beginning andend but not in the interior. Yet another NA routine can be found inthe stinepack package where na.stinterpperforms Stineman interpolation.

A typical task to be performed on ordered observations is to evaluate somefunction, e.g., computing the mean, in a window of observations that is movedover the full sample period. The generic function rollapplyprovides this functionality for arbitrary functions and more efficient versionsrollmean, rollmax, rollmedian areavailable for the mean, maximum and median respectively.

The zoo package has an as.Date numeric methodwhich is similar to the one in the core of R except that theorigin argument defaults to January 1, 1970 (whereas the onein the core of R has no default).

Note that since zoo uses date/time classes from base R and other packages, it may inherit bugs or problems with those date/time classes.Currently, there is one such known problem with the c method forthe POSIXct class in base R:If x and y are POSIXct objects with tzoneattributes, the attribute will always be dropped in c(x, y), evenif it is the same across both x and y. Although this is documentedat c.POSIXct, one may want to employ a workaroundas shown at https://stat.ethz.ch/pipermail/r-devel/2010-August/058112.html.

zoo function - RDocumentation (2024)
Top Articles
Latest Posts
Article information

Author: Aron Pacocha

Last Updated:

Views: 6195

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Aron Pacocha

Birthday: 1999-08-12

Address: 3808 Moen Corner, Gorczanyport, FL 67364-2074

Phone: +393457723392

Job: Retail Consultant

Hobby: Jewelry making, Cooking, Gaming, Reading, Juggling, Cabaret, Origami

Introduction: My name is Aron Pacocha, I am a happy, tasty, innocent, proud, talented, courageous, magnificent person who loves writing and wants to share my knowledge and understanding with you.