Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aggregating daily content

Tags:

r

aggregate

I've been attempting to aggregate (some what erratic) daily data. I'm actually working with csv data, but if i recreate 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'd like to aggregate this on a daily basis (notice that some times there are >1 datapoint for a day, and sometimes there arent.

I've tried lots and lots of variations, but i cant seem to aggregate, so for instance this fails:

aggregate(z, as.Date(time(z)), sum)
# Error in Summary.factor(2:3, na.rm = FALSE) : sum not meaningful for factors

There seems to be a lot of content regarding aggregate, and i've tried a number of versions but cant seem to sum this on a daily level. I'd also like to run cummax and cumulative averages in addition to the daily summing.

Any help woudl be greatly appreciated.

Update

The code I am actually using 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 my (unintentional) quotation of the numbers above is similar to what is happening in practice, because when I do:

aggregate(z, index(z), sum)
#Error in Summary.factor(25L, na.rm = FALSE) : sum not meaningful for factors

There a number of columns (100 or so), how can i specify them to be as.numeric automatically ? (stringAsFactors = False doesnt appear to work?)

like image 951
Malang Avatar asked Nov 21 '25 16:11

Malang


1 Answers

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:)

like image 169
Henrik Avatar answered Nov 23 '25 08:11

Henrik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!