Re: [R] building time series/zoo/its from a data frame

From: Dennis Murphy <djmuser_at_gmail.com>
Date: Wed, 02 Jun 2010 09:19:53 -0700

Hi:

Here's one approach, converting the sub-data frames to zoo objects:

# Function to convert one of the data frames to a zoo object makezoo <- function(df) {

     require(zoo)
     date <- as.Date(df[, 1], format = '%m/%d/%y')
     with(df, zoo(freq, date))

    }
library(zoo)

# Split the data frame by condition:
tt <- split(x.df[, -2], x.df$cond)

# > str(tt[[1]])
# 'data.frame':   5 obs. of  2 variables:
#  $ date: Factor w/ 5 levels "04/01/09","04/02/09",..: 1 2 3 4 5
#  $ freq: int  12 11 10 13 6

# Apply the makezoo function to each component of the list tt: (tser <- lapply(tt, makezoo))

$Fever
04/01/09 04/02/09 04/03/09 04/04/09 04/05/09

      12 11 10 13 6

$Rash
04/01/09 04/02/09 04/03/09 04/04/09 04/05/09

       6 10 9 10 8

$Respiratory
04/01/09 04/02/09 04/03/09 04/04/09 04/05/09

      12 9 6 11 11

# Check that the classes are OK:
> sapply(tser, class)

      Fever        Rash Respiratory
      "zoo"       "zoo"       "zoo"

You can then do things like plot.ts, for example:

lapply(tser, plot.ts)

HTH,
Dennis

On Wed, Jun 2, 2010 at 8:37 AM, Erin Hodgess <erinm.hodgess_at_gmail.com>wrote:

> Dear R People:

>

> I have the following data frame:
>

> > x.df
> date cond freq
> 1 04/01/09 Fever 12
> 2 04/02/09 Fever 11
> 3 04/03/09 Fever 10
> 4 04/04/09 Fever 13
> 5 04/05/09 Fever 6
> 6 04/01/09 Rash 6
> 7 04/02/09 Rash 10
> 8 04/03/09 Rash 9
> 9 04/04/09 Rash 10
> 10 04/05/09 Rash 8
> 11 04/01/09 Respiratory 12
> 12 04/02/09 Respiratory 9
> 13 04/03/09 Respiratory 6
> 14 04/04/09 Respiratory 11
> 15 04/05/09 Respiratory 11
> >
>

> I would like to generate 3 time series (or zoo objects or its); one
> for Fever, one for Rash, and one for Respiratory. There are 2
> questions here, please:
> a. How do I generate the series, please?
> b. Which is best, time series, zoo objects, or its objects, please?
>

> Thanks,
> Erin
>
>

> --
> Erin Hodgess
> Associate Professor
> Department of Computer and Mathematical Sciences
> University of Houston - Downtown
> mailto: erinm.hodgess_at_gmail.com
>

> ______________________________________________
> 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.
>

        [[alternative HTML version deleted]]



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. Received on Wed 02 Jun 2010 - 16:34:56 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 02 Jun 2010 - 21:20:26 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive