[R] ZOO objects: Creating, Plotting, Analyzing

Achim Zeileis Achim.Zeileis at uibk.ac.at
Thu Mar 20 02:31:43 CET 2014


On Wed, 19 Mar 2014, Rich Shepard wrote:

>  I need to plot and analyze irregular time series. Despite reading the 2005
> JSS article on zoo, ?zoo, ?plot.zoo, and the quickref vignette I'm still not
> doing this correctly. dput() output of a matrix and the zoo object are at
> the end of this message.

It's sufficient to send such messages once even if others don't reply 
within minutes.

>  My work flow is to read the *.csv file to create a data.frame, subset 
> the df to yield a date/data file for each constituent, convert the 
> subset df to a matrix using as.matrix() (see s95.ec.txt attached), then 
> create a zoo object specifying 'sampdate' as the order.by index. The 
> result is the second attachment (s95.ec.z.txt) which does not appear 
> correct to me.

Please have a look at read.zoo() and the accompanying "read.zoo" vignette 
which does exactly what you are looking for (reading .csv files and 
turning it into a zoo series).

>  Of the several zoo methods involving NA values, none of the imputations
> seem appropriate for my data as there is neither pattern or sufficiently
> short gaps between observed values, so I chose 'na.omit.' Putting that in
> the command, e.g., s95.ec.z <- zoo(s95.ec, order.by = 'sampdate', na.omit)
> produced an error:
>
> s95.ec.z <- zoo(s95.ec, order.by = 'sampdate', na.omit)
> Error in frequency > 1 :
>  comparison (6) is possible only for atomic and list types
>
>  I suspect that the first hurdle I need to overcome is correctly 
> creating a zoo object from the matrix.

Yes. The first argument should be a numeric vector or matrix and the 
second argument should be something suitable as a time index. In your case 
s95.ec is a data.frame with two "character" columns which is not suitable 
directly. This works:

s95.ec.z <- zoo(as.numeric(s95.ec[,2]), order.by = as.Date(s95.ec[,1]))
plot(s95.ec.z)

But the route via read.zoo should be more convenient, I guess.

>  All the data I analyze have irregular collection dates and I need to
> become fluent with zoo objects so I can correctly analyze and plot the time
> series of these data.
>
> Rich
>
> <s95.ec.txt>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> structure(c("1992-03-12", "1992-04-22", "1992-05-22", "1992-06-03", 
> "1992-11-06", "1993-01-28", "1993-02-18", "1993-03-03", "1993-03-15", 
> "1993-04-08", "1993-04-30", "1993-05-11", "1993-05-27", "1993-06-10", 
> "1993-06-25", "1993-07-09", "1993-07-31", "1994-03-28", "1994-04-07", 
> "1994-04-26", "1994-05-27", "1994-06-23", "1995-04-11", "1995-04-29", 
> "1995-05-30", "1995-06-23", "1995-07-26", "1995-12-04", "1996-01-16", 
> "1996-05-07", "1996-06-05", "1996-07-02", "1997-04-30", "1997-05-14", 
> "1997-06-16", "1997-07-29", "1998-05-25", "1998-05-28", "1998-07-27", 
> "1998-09-15", "1998-10-21", "1998-11-24", "1999-05-10", "1999-05-25", 
> "1999-06-22", "1999-07-22", "1999-11-09", "1999-12-01", "2000-03-04", 
> "2000-03-10", "2000-04-21", "2000-05-09", "2000-06-28", "2000-11-27", 
> "2000-12-07", "2001-02-16", "2001-03-07", "2001-04-30", "2001-05-11", 
> "2001-06-25", "2002-05-04", "2002-06-21", "2003-05-03", "2003-06-15", 
> "2004-06-28", "2004-07-01", " 85", " 80", " 85", NA, " 90", "110", " 90", 
> "100", "875", " 85", " 83", " 75", " 49", " 65", " 57", " 55", " 64", " 60", 
> " 80", " 80", " 45", " 65", " 83", " 60", " 40", " 62", " 80", "119", " 55", 
> " 65", " 44", " 48", " 68", " 43", " 50", " 70", " 52", " 45", " 66", " 92", 
> " 92", "134", " 77", " 52", "382", " 72", " 83", " 78", " 78", " 77", " 62", 
> " 51", " 59", " 79", " 65", " 77", " 81", " 59", " 55", " 67", " 67", " 16", 
> " 70", NA, " 42", " 42"), .Dim = c(66L, 2L), .Dimnames = list(
>    c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
>    "12", "13", "14", "15", "16", "17", "18", "19", "20", "21",
>    "22", "23", "24", "25", "26", "27", "28", "29", "30", "31",
>    "32", "33", "34", "35", "36", "37", "38", "39", "40", "41",
>    "42", "43", "44", "45", "46", "47", "48", "49", "50", "51",
>    "52", "53", "54", "55", "56", "57", "58", "59", "60", "61",
>    "62", "63", "64", "65", "66"), c("sampdate", "EC")))
>
> <s95.ex.z.txt>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> structure(c("1992-03-12", " 85"), .Dim = 1:2, .Dimnames = list(
>    "1", c("sampdate", "EC")), index = "sampdate", class = "zoo")
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list