[R] Using zoo() to aggregate daily data to monthly means

D Kelly O'Day koday at processtrends.com
Tue Dec 22 20:12:19 CET 2009

I am trying to get monthly means for a daily data series using zoo(). I have
found an odd problem, that seems to be caused by zoo()'s handling of leap

Here's my R script with 2 methods (freq=365, 366) for aggregating the daily
data to monthly series:

J_link <- "http://www.ijis.iarc.uaf.edu/seaice/extent/plot.csv"
JAXA_data <- read.table(J_link,
             skip = 0, sep = ",", dec=".",
             row.names = NULL, header = FALSE,
             as.is = T, colClasses = rep("numeric",4),
             comment.char = "#", na.strings = c("*", "-",-99.9, -9999),
             col.names = c("Mo", "Day", "Yr", "Extent") )
## Subset raw data to period: Jan,2003 to Dec, 2007 
 JAXA <- subset(JAXA_data, JAXA_data$Yr >=2003 & JAXA_data$Yr <=2007)
## create zoo object starting Jan, 2003 - use freq's of 365 and 366
 JAXA_365 <- as.zoo(ts(JAXA$Extent, start = c(2003,1,1),freq=365))
 JAXA_366 <- as.zoo(ts(JAXA$Extent, start = c(2003,1,1),freq=366))
## aggregate to yearmon using JAXA_365 & JAXA_366 zoo objects
 JAXA_mo_365 <- aggregate(JAXA_365, mean, by=yearmon,  na.rm=T)
 JAXA_mo_366 <- aggregate(JAXA_366, mean, by=yearmon,  na.rm=T)
## Compare last 6 records for JAXA_365 & JAXA_366

When I compare the two tail reports, I get Jan, 2007 for last month for
JAXA_365 and Dec, 2007 for JAXA_366.

What is proper freq for daily data, 365 or 366 or other? I have seen many
examples that use 365 for ts, I assumed zoo() worked the same.

What am I missing?

View this message in context: http://n4.nabble.com/Using-zoo-to-aggregate-daily-data-to-monthly-means-tp977263p977263.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list