I am trying to combine (some kind of erratic) daily data. I actually work with csv data, but if I recreated it, it would look something like this:
library(zoo) dates <- c("20100505", "20100505", "20100506", "20100507") val1 <- c("10", "11", "1", "6") val2 <- c("5", "31", "2", "7") x <- data.frame(dates = dates, val1=val1, val2=val2) z <- read.zoo(x, format = "%Y%m%d")
Now I would like to summarize this on a daily basis (note that in some cases there is 1 date-point during the day, and sometimes arent.
I tried many, many options, but I canβt compose, so for example this fails:
aggregate(z, as.Date(time(z)), sum)
There seems to be a lot of content regarding the aggregate, and I tried several versions, but it doesn't seem to summarize this on a daily level. I would also like to run cummax and cumulative averages in addition to daily totals.
Any help would be appreciated.
Update
The code I use is as follows:
z <- read.zoo(file = "data.csv", sep = ",", header = TRUE, stringsAsFactors = FALSE, blank.lines.skip = T, na.strings="NA", format = "%Y%m%d");
It seems that my (inadvertent) quote from the above numbers is similar to what happens in practice, because when I do this:
aggregate(z, index(z), sum) #Error in Summary.factor(25L, na.rm = FALSE) : sum not meaningful for factors
There are a few columns (100 or so), how can I indicate that they will be like .numeric automatically? ( stringAsFactors = False doesn't seem to work?)