Dear R People: I have the following data frame:> x.dfdate 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
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@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@gmail.com > > ______________________________________________ > R-help@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]]
Gabor Grothendieck
2010-Jun-02 21:00 UTC
[R] building time series/zoo/its from a data frame
r-help seems to have rejected the first attempt at sending this so I am trying again. read.zoo has a split= argument which lets you read in the data and split it all at the same time. You can also use it on a data frame. Due to a bug which is fixed in the development version, you will need the development version of zoo which you can access as shown below. First we show reading it in using read.zoo and splitting it at the same time. Next we assume its already been read in as a data frame in which case we can also use read.zoo on a data.frame: library(zoo) source("http://r-forge.r-project.org/scm/viewvc.php/*checkout*/pkg/zoo/R/read.zoo.R?revision=717&root=zoo") Lines <- "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" # 1. reading it in and splitting at the same time z <- read.zoo(textConnection(Lines), header = TRUE, split = "cond", format = "%m/%d/%y") # or, 2. if its already a data frame such as this one we can still use read.zoo DF <- read.table(textConnection(Lines), header = TRUE) z2 <- read.zoo(DF, split = 2, format = "%m/%d/%y")