mentby.com
Blog | Jobs | Help | Signup | Login

Creating ZOO Matrix from Data Frame



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


Rich Shepard Mon, 02 Jan 2012 08:33:51 -0800

I created a new data frame for a single stream and it has this structure:

str(burns.tds)
'data.frame':    2472 obs. of  3 variables:
  $ site    : Factor w/ 137 levels "BC-0.5","BC-1",..: 5 5 5 5 5 5 5 5 ...
  $ sampdate: Date, format: "1992-03-27" "1992-04-30" ...
  $ quant   : num  0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 8.08 ...

This is in 'long' form with the date as the second column:

head(burns.tds)
      site   sampdate quant
599  BC-3 1992-03-27   0.1
600  BC-3 1992-04-30   0.1
601  BC-3 1992-05-30   0.1
603  BC-3 1992-06-19   0.1
1214 BC-3 1992-07-20   0.1
1215 BC-3 1992-08-10   0.1

   I've read the read.zoo help and above URL and cannot get the syntax
correct. Emulating the example at the bottom of the referenced Web page
produces an error that I interpret as a need to exclude NAs. Adding na.omit
to the command still produces an error:

burns.tds.z <- read.zoo(burns.tds, split = 1, format = "%Y-%m-%d", na.omit)
Error in strptime(format(x, scientific = FALSE), tz = tz, format = format):
   invalid 'tz' value

   How do I resolve this invalid 'tz' value? Or, is there another problem but
this is the error presented to me?

Rich


Rich Shepard Mon, 02 Jan 2012 13:14:18 -0800



Related Topics

Post a Comment