[R] Using zoo() to aggregate daily data to monthly means
Achim Zeileis
Achim.Zeileis at wu-wien.ac.at
Tue Dec 22 20:29:58 CET 2009
On Tue, 22 Dec 2009, D Kelly O'Day wrote:
>
> 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
> years.
It's not really zoo's odd handling, but yours ;-) More seriously, do not
use ts() with either freq = 365 or 366 to represent daily observations.
zoo with "Date" index is more suitable (or the corresponding xts).
> Here's my R script with 2 methods (freq=365, 366) for aggregating the daily
> data to monthly series:
>
> library(zoo)
> 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))
As explained above, I would use a "Date" index:
JAXA_daily <- with(JAXA_data,
zoo(Extent, as.Date(paste(Yr, Mo, Day, sep = "-")))
)
and then you can do
JAXA_monthly <- aggregate(JAXA_daily, as.yearmon, mean, na.rm = TRUE)
which should give what you expected.
See zoo's package vignettes
vignette(package = "zoo")
for more details.
hth,
Z
> ## 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
> tail(JAXA_mo_365)
> tail(JAXA_mo_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.
>
> ______________________________________________
> R-help at 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.
>
>
More information about the R-help
mailing list