[R] Creating ZOO Matrix from Data Frame

Rich Shepard rshepard at appl-ecosys.com
Mon Jan 2 17:31:38 CET 2012


   I believe that I have a basic understanding of zoo and how to use read.zoo
on a text file, What I have not seen in the zoo help files and vignettes is
how to convert a data frame to a zoo matrix for irregular time series
analyses.

   An example data frame is structured like this:

str(burns.cast)
'data.frame':	256 obs. of  47 variables:
  $ site     : Factor w/ 143 levels "BC-0.5","BC-1",..: 1 1 1 2 2 2 2 2 2 2
  $ sampdate : Date, format: "1996-04-19" "1996-05-21" ...
  $ Ca       : num  76.6 NA NA NA NA ...
  $ Cond     : num  712 403 731 NA NA NA NA NA NA NA ...
  $ HCO3     : num  162 152 212 NA NA NA NA NA NA NA ...
  $ SO4      : num  175 57 194 NA NA NA NA NA NA NA ...
  $ TDS      : num  460 212 530 NA NA NA NA NA NA NA ...
  ...

   What I think I need is a matrix that orders sampdate, has the numeric
values for TDS, and retains the factor of site (so that I can compare TDS
values on a given date by site). The August 2011 version of zoo docs
suggests that I need to modify the above from a data frame to a matrix, but
there is more than a single class involved.

   Would something like this work?

z <- zoo(burns.cast$TDS, order.by = burns.cast$sampdate, style = vertical)

And where do I include the site factor?

   A pointer to docs or an example will be much appreciated.

Rich



More information about the R-help mailing list