EVOLUTION-MANAGER
Edit File: zoo.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Z's Ordered Observations</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for zoo {zoo}"><tr><td>zoo {zoo}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Z's Ordered Observations</h2> <h3>Description</h3> <p><code>zoo</code> is the creator for an S3 class of indexed totally ordered observations which includes irregular time series. </p> <h3>Usage</h3> <pre> zoo(x = NULL, order.by = index(x), frequency = NULL, calendar = getOption("zoo.calendar", TRUE)) ## S3 method for class 'zoo' print(x, style = , quote = FALSE, ...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>a numeric vector, matrix or a factor.</p> </td></tr> <tr valign="top"><td><code>order.by</code></td> <td> <p>an index vector with unique entries by which the observations in <code>x</code> are ordered. See the details for support of non-unique indexes.</p> </td></tr> <tr valign="top"><td><code>frequency</code></td> <td> <p>numeric indicating frequency of <code>order.by</code>. If specified, it is checked whether <code>order.by</code> and <code>frequency</code> comply. If so, a regular <code>"zoo"</code> series is returned, i.e., an object of class <code>c("zooreg", "zoo")</code>. See below and <code><a href="zooreg.html">zooreg</a></code> for more details.</p> </td></tr> <tr valign="top"><td><code>calendar</code></td> <td> <p>logical. If <code>frequency</code> is specified and is 4 or 12: Should <code><a href="yearqtr.html">yearqtr</a></code> or <code><a href="yearmon.html">yearmon</a></code> be used for a numeric index <code>order.by</code>?</p> </td></tr> <tr valign="top"><td><code>style</code></td> <td> <p>a string specifying the printing style which can be <code>"horizontal"</code> (the default for vectors), <code>"vertical"</code> (the default for matrices) or <code>"plain"</code> (which first prints the data and then the index).</p> </td></tr> <tr valign="top"><td><code>quote</code></td> <td> <p>logical. Should characters be quoted?</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>further arguments passed to the print methods of the data and the index.</p> </td></tr> </table> <h3>Details</h3> <p><code>zoo</code> 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 <code>NROW(x)</code> 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 <code>order.by</code>). In principle, the data <code>x</code> could also be arbitrary, but currently there is only support for vectors and matrices and partial support for factors. </p> <p><code>zoo</code> is particularly aimed at irregular time series of numeric vectors/matrices, but it also supports regular time series (i.e., series with a certain <code>frequency</code>). <code>zoo</code>'s key design goals are independence of a particular index/date/time class and consistency with <code>ts</code> and base <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> by providing methods to standard generics. Therefore, standard functions can be used to work with <code>"zoo"</code> objects and memorization of new commands is reduced. </p> <p>When creating a <code>"zoo"</code> object with the function <code>zoo</code>, the vector of indexes <code>order.by</code> can be of (a single) arbitrary class (if <code>x</code> is shorter or longer than <code>order.by</code> it is expanded accordingly), but it is essential that <code>ORDER(order.by)</code> works. For other functions it is assumed that <code>c()</code>, <code>length()</code>, <code>MATCH()</code> and subsetting <code>[,</code> 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 <code><a href="ORDER.html">ORDER</a></code> and <code><a href="MATCH.html">MATCH</a></code> are created in the <code>zoo</code> package with default methods corresponding to the non-generic base functions <code><a href="../../base/html/order.html">order</a></code> and <code><a href="../../base/html/match.html">match</a></code>. Note that the <code><a href="../../base/html/order.html">order</a></code> and hence the default <code><a href="ORDER.html">ORDER</a></code> typically work if there is a <code><a href="../../base/html/xtfrm.html">xtfrm</a></code> method. Furthermore, for certain (but not for all) operations the index class should have an <code>as.numeric</code> method (in particular for regular series) and an <code>as.character</code> method might improve printed output (see also below). </p> <p>The index observations <code>order.by</code> should typically be unique, such that the observations can be totally ordered. Nevertheless, <code>zoo()</code> is able to create <code>"zoo"</code> objects with duplicated indexes (with a warning) and simple methods such as <code>plot()</code> or <code>summary()</code> will typically work for such objects. However, this is not formally supported as the bulk of functionality provided in <span class="pkg">zoo</span> requires unique index observations/time stamps. See below for an example how to remove duplicated indexes. </p> <p>If a <code>frequency</code> is specified when creating a series via <code>zoo</code>, the object returned is actually of class <code>"zooreg"</code> which inherits from <code>"zoo"</code>. This is a subclass of <code>"zoo"</code> which relies on having a <code>"zoo"</code> series with an additional <code>"frequency"</code> attribute (which has to comply with the index of that series). Regular <code>"zooreg"</code> series can also be created by <code><a href="zooreg.html">zooreg</a></code>, the <code>zoo</code> analogue of <code><a href="../../stats/html/ts.html">ts</a></code>. See the respective help page and <code><a href="is.regular.html">is.regular</a></code> for further details. </p> <p>Methods to standard generics for <code>"zoo"</code> objects currently include: <code>print</code> (see above), <code>summary</code>, <code>str</code>, <code>head</code>, <code>tail</code>, <code>[</code> (subsetting), <code>rbind</code>, <code>cbind</code>, <code>merge</code> (see <code><a href="merge.zoo.html">merge.zoo</a></code>), <code>aggregate</code> (see <code><a href="aggregate.zoo.html">aggregate.zoo</a></code>), <code>rev</code>, <code>split</code> (see <code><a href="aggregate.zoo.html">aggregate.zoo</a></code>), <code>barplot</code>, <code>plot</code> and <code>lines</code> (see <code><a href="plot.zoo.html">plot.zoo</a></code>). For multivariate <code>"zoo"</code> series with column names the <code>$</code> extractor is available, behaving similar as for <code>"data.frame"</code> objects. Methods are also available for <code>median</code> and <code>quantile</code>. </p> <p><code>ifelse.zoo</code> is not a method (because <code>ifelse</code> is not a generic) but must be written out including the <code>.zoo</code> suffix. </p> <p>To “prettify” printed output of <code>"zoo"</code> series the generic function <code>index2char</code> is used for turning index values into character values. It defaults to using <code>as.character</code> but can be customized if a different printed display should be used (although this should not be necessary, usually). </p> <p>The subsetting method <code>[</code> work essentially like the corresponding functions for vectors or matrices respectively, i.e., takes indexes of type <code>"numeric"</code>, <code>"integer"</code> or <code>"logical"</code>. But additionally, it can be used to index with observations from the index class of the series. If the index class of the series is one of the three classes above, the corresponding index has to be encapsulated in <code>I()</code> to enforce usage of the index class (see examples). Subscripting by a zoo object whose data contains logical values is undefined. </p> <p>Additionally, <code>zoo</code> provides several generic functions and methods to work (a) on the data contained in a <code>"zoo"</code> object, (b) the index (or time) attribute associated to it, and (c) on both data and index: </p> <p>(a) The data contained in <code>"zoo"</code> objects can be extracted by <code>coredata</code> (strips off all <code>"zoo"</code>-specific attributes) and modified using <code>coredata<-</code>. Both are new generic functions with methods for <code>"zoo"</code> objects, see <code><a href="coredata.html">coredata</a></code>. </p> <p>(b) The index associated with a <code>"zoo"</code> object can be extracted by <code>index</code> and modified by <code>index<-</code>. As the interpretation of the index as “time” in time series applications is more natural, there are also synonymous methods <code>time</code> and <code>time<-</code>. The start and the end of the index/time vector can be queried by <code>start</code> and <code>end</code>. See <code><a href="index.html">index</a></code>. </p> <p>(c) To work on both data and index/time, <code>zoo</code> provides methods <code>lag</code>, <code>diff</code> (see <code><a href="lag.zoo.html">lag.zoo</a></code>) and <code>window</code>, <code>window<-</code> (see <code><a href="window.zoo.html">window.zoo</a></code>). </p> <p>In addition to standard group generic function (see <code><a href="../../methods/html/S4groupGeneric.html">Ops</a></code>), the following mathematical operations are available as methods for <code>"zoo"</code> objects: transpose <code>t</code> which coerces to a matrix first, and <code>cumsum</code>, <code>cumprod</code>, <code>cummin</code>, <code>cummax</code> which are applied column wise. </p> <p>Coercion to and from <code>"zoo"</code> objects is available for objects of various classes, in particular <code>"ts"</code>, <code>"irts"</code> and <code>"its"</code> objects can be coerced to <code>"zoo"</code>, the reverse is available for <code>"its"</code> and for <code>"irts"</code> (the latter in package <code>tseries</code>). Furthermore, <code>"zoo"</code> objects can be coerced to vectors, matrices and lists and data frames (dropping the index/time attribute). See <code><a href="as.zoo.html">as.zoo</a></code>. </p> <p>Several methods are available for <code>NA</code> handling in the data of <code>"zoo"</code> objects: <code><a href="na.aggregate.html">na.aggregate</a></code> which uses group means to fill in <code>NA</code> values, <code><a href="na.approx.html">na.approx</a></code> which uses linear interpolation to fill in <code>NA</code> values. <code><a href="zoo.html">na.contiguous</a></code> which extracts the longest consecutive stretch of non-missing values in a <code>"zoo"</code> object, <code><a href="na.fill.html">na.fill</a></code> which uses fixed specified values to replace <code>NA</code> values, <code><a href="na.locf.html">na.locf</a></code> which replaces <code>NA</code>s by the last previous non-<code>NA</code>, <code><a href="../../stats/html/na.fail.html">na.omit</a></code> which returns a <code>"zoo"</code> object with incomplete observations removed, <code><a href="na.approx.html">na.spline</a></code> which uses spline interpolation to fill in <code>NA</code> values and <code><a href="na.StructTS.html">na.StructTS</a></code> which uses a seasonal Kalman filter to fill in <code>NA</code> values, <code><a href="na.trim.html">na.trim</a></code> which trims runs of <code>NA</code>s off the beginning and end but not in the interior. Yet another <code>NA</code> routine can be found in the <code>stinepack</code> package where <code>na.stinterp</code> performs Stineman interpolation. </p> <p>A typical task to be performed on ordered observations is to evaluate some function, e.g., computing the mean, in a window of observations that is moved over the full sample period. The generic function <code><a href="rollapply.html">rollapply</a></code> provides this functionality for arbitrary functions and more efficient versions <code><a href="rollmean.html">rollmean</a></code>, <code><a href="rollmean.html">rollmax</a></code>, <code><a href="rollmean.html">rollmedian</a></code> are available for the mean, maximum and median respectively. </p> <p>The <span class="pkg">zoo</span> package has an <code>as.Date</code> <code>numeric</code> method which is similar to the one in the core of <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> except that the <code>origin</code> argument defaults to January 1, 1970 (whereas the one in the core of <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> has no default). </p> <p>Note that since <code>zoo</code> uses date/time classes from base <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> and other packages, it may inherit bugs or problems with those date/time classes. Currently, there is one such known problem with the <code>c</code> method for the <code>POSIXct</code> class in base <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span>: If <code>x</code> and <code>y</code> are <code>POSIXct</code> objects with <code>tzone</code> attributes, the attribute will always be dropped in <code>c(x, y)</code>, even if it is the same across both <code>x</code> and <code>y</code>. Although this is documented at <code><a href="../../base/html/DateTimeClasses.html">c.POSIXct</a></code>, one may want to employ a workaround as shown at <a href="https://stat.ethz.ch/pipermail/r-devel/2010-August/058112.html">https://stat.ethz.ch/pipermail/r-devel/2010-August/058112.html</a>. </p> <h3>Value</h3> <p>A vector or matrix with an <code>"index"</code> attribute of the same dimension (<code>NROW(x)</code>) by which <code>x</code> is ordered. </p> <h3>References</h3> <p>Achim Zeileis and Gabor Grothendieck (2005). <span class="pkg">zoo</span>: S3 Infrastructure for Regular and Irregular Time Series. <em>Journal of Statistical Software</em>, <b>14(6)</b>, 1-27. URL http://www.jstatsoft.org/v14/i06/ and available as <code>vignette("zoo")</code>. </p> <p>Ajay Shah, Achim Zeileis and Gabor Grothendieck (2005). <span class="pkg">zoo</span> Quick Reference. Package vignette available as <code>vignette("zoo-quickref")</code>. </p> <h3>See Also</h3> <p><code><a href="zooreg.html">zooreg</a></code>, <code><a href="plot.zoo.html">plot.zoo</a></code>, <code><a href="index.html">index</a></code>, <code><a href="merge.zoo.html">merge.zoo</a></code></p> <h3>Examples</h3> <pre> suppressWarnings(RNGversion("3.5.0")) set.seed(1) ## simple creation and plotting x.Date <- as.Date("2003-02-01") + c(1, 3, 7, 9, 14) - 1 x <- zoo(rnorm(5), x.Date) plot(x) time(x) ## subsetting with numeric indexes x[c(2, 4)] ## subsetting with index class x[as.Date("2003-02-01") + c(2, 8)] ## different classes of indexes/times can be used, e.g. numeric vector x <- zoo(rnorm(5), c(1, 3, 7, 9, 14)) ## subsetting with numeric indexes then uses observation numbers x[c(2, 4)] ## subsetting with index class can be enforced by I() x[I(c(3, 9))] ## visualization plot(x) ## or POSIXct y.POSIXct <- ISOdatetime(2003, 02, c(1, 3, 7, 9, 14), 0, 0, 0) y <- zoo(rnorm(5), y.POSIXct) plot(y) ## create a constant series z <- zoo(1, seq(4)[-2]) ## create a 0-dimensional zoo series z0 <- zoo(, 1:4) ## create a 2-dimensional zoo series z2 <- zoo(matrix(1:12, 4, 3), as.Date("2003-01-01") + 0:3) ## create a factor zoo object fz <- zoo(gl(2,5), as.Date("2004-01-01") + 0:9) ## create a zoo series with 0 columns z20 <- zoo(matrix(nrow = 4, ncol = 0), 1:4) ## arithmetic on zoo objects intersects them first x1 <- zoo(1:5, 1:5) x2 <- zoo(2:6, 2:6) 10 * x1 + x2 ## $ extractor for multivariate zoo series with column names z <- zoo(cbind(foo = rnorm(5), bar = rnorm(5))) z$foo z$xyz <- zoo(rnorm(3), 2:4) z ## add comments to a zoo object comment(x1) <- c("This is a very simple example of a zoo object.", "It can be recreated using this R code: example(zoo)") ## comments are not output by default but are still there x1 comment(x1) # ifelse does not work with zoo but this works # to create a zoo object which equals x1 at # time i if x1[i] > x1[i-1] and 0 otherwise (diff(x1) > 0) * x1 ## zoo series with duplicated indexes z3 <- zoo(1:8, c(1, 2, 2, 2, 3, 4, 5, 5)) plot(z3) ## remove duplicated indexes by averaging lines(aggregate(z3, index(z3), mean), col = 2) ## or by using the last observation lines(aggregate(z3, index(z3), tail, 1), col = 4) ## x1[x1 > 3] is not officially supported since ## x1 > 3 is of class "zoo", not "logical". ## Use one of these instead: x1[which(x1 > 3)] x1[coredata(x1 > 3)] x1[as.logical(x1 > 3)] subset(x1, x1 > 3) ## any class supporting the methods discussed can be used ## as an index class. Here are examples using complex numbers ## and letters as the time class. z4 <- zoo(11:15, complex(real = c(1, 3, 4, 5, 6), imag = c(0, 1, 0, 0, 1))) merge(z4, lag(z4)) z5 <- zoo(11:15, letters[1:5]) merge(z5, lag(z5)) # index values relative to 2001Q1 zz <- zooreg(cbind(a = 1:10, b = 11:20), start = as.yearqtr(2000), freq = 4) zz[] <- mapply("/", as.data.frame(zz), coredata(zz[as.yearqtr("2001Q1")])) ## even though time index must be unique zoo (and read.zoo) ## will both allow creation of such illegal objects with ## a warning (rather than ana error) to give the user a ## chance to fix them up. Extracting and replacing times ## and aggregate.zoo will still work. ## Not run: # this gives a warning # and then creates an illegal zoo object z6 <- zoo(11:15, c(1, 1, 2, 2, 5)) z6 # fix it up by averaging duplicates aggregate(z6, identity, mean) # or, fix it up by taking last in each set of duplicates aggregate(z6, identity, tail, 1) # fix it up via interpolation of duplicate times time(z6) <- na.approx(ifelse(duplicated(time(z6)), NA, time(z6)), na.rm = FALSE) # if there is a run of equal times at end they # wind up as NAs and we cannot have NA times z6 <- z6[!is.na(time(z6))] z6 x1. <- x1 <- zoo (matrix (1:12, nrow = 3), as.Date("2008-08-01") + 0:2) colnames (x1) <- c ("A", "B", "C", "D") x2 <- zoo (matrix (1:12, nrow = 3), as.Date("2008-08-01") + 1:3) colnames (x2) <- c ("B", "C", "D", "E") both.dates = as.Date (intersect (index (t1), index (t2))) both.cols = intersect (colnames (t1), colnames (t2)) x1[both.dates, both.cols] ## there is "[.zoo" but no "[<-.zoo" however four of the following ## five examples work ## wrong ## x1[both.dates, both.cols] <- x2[both.dates, both.cols] # 4 correct alternatives # #1 window(x1, both.dates)[, both.cols] <- x2[both.dates, both.cols] # #2. restore x1 and show a different way x1 <- x1. window(x1, both.dates)[, both.cols] <- window(x2, both.dates)[, both.cols] # #3. restore x1 and show a different way x1 <- x1. x1[time(x1) # #4. restore x1 and show a different way x1 <- x1. x1[time(x1) ## End(Not run) </pre> <hr /><div style="text-align: center;">[Package <em>zoo</em> version 1.8-11 <a href="00Index.html">Index</a>]</div> </body></html>