tags:

views:

78

answers:

3
+2  A: 

You started on the right path but made a couple of mistakes.

First, zoo only consumes matrices, not data.frames. Second, those need numeric inputs:

> z <- zoo(as.matrix(data.frame(val1=c(10,11,1,6), val2=c(5,31,2,7))), 
+          order.by=as.Date(c("20100505","20100505","20100506","20100507"),
+                           "%Y%m%d"))
Warning message:
In zoo(as.matrix(data.frame(val1 = c(10, 11, 1, 6), val2 = c(5,  :
  some methods for "zoo" objects do not work if the index entries in 
  'order.by' are not unique

This gets us a warning which is standard in zoo: it does not like identical time indices.

Always a good idea to show the data structure, maybe via str() as well, maybe run summary() on it:

> z
           val1 val2
2010-05-05   10    5
2010-05-05   11   31
2010-05-06    1    2
2010-05-07    6    7

And then, once we have it, aggregation is easy:

> aggregate(z, index(z), sum)
           val1 val2
2010-05-05   21   36
2010-05-06    1    2
2010-05-07    6    7
> 
Dirk Eddelbuettel
But `zoo` consumed my data.frame! (because `read.zoo` works on data.frames) ;-)
Joshua Ulrich
Thank you - I think i am a couple of steps closer now :) made a silly mistake with quoting the numbers there, when i was making up the example, but actually, it seems that i what is happening in the real life code too. I've updated the question, still having a bit of trouble..
Malang
+1  A: 

val1 and val2 are character strings. data.frame() converts them to factors. Summing factors doesn't make sense. You probably intended:

x <- data.frame(dates = dates, val1=as.numeric(val1), val2=as.numeric(val2))
z <- read.zoo(x, format = "%Y%m%d")
aggregate(z, as.Date(time(z)), sum)

which yields:

           val1 val2
2010-05-05   21   36
2010-05-06    1    2
2010-05-07    6    7
Joshua Ulrich
Thanks Joshua - is it possible to specify them all as.numeric (if importing a csv ?) i've updated the question, i suppose the first one was too simplified :)
Malang
+3  A: 

Or you aggregate before using zoo (val1 and val2 need to be numeric though).

x <- data.frame(dates = dates, val1=as.numeric(val1), val2=as.numeric(val2))
y <- aggregate(x[,2:3],by=list(x[,1]),FUN=sum)

and then feed y into zoo.

You avoid the warning:)

Henrik
Sweet - I was able to get this to work for the CSV data too (eventually :) thanks!
Malang