Hi,  

I found that apply.monthly() in xts does not work as I expected in the case of 
a sparse timeseries:

my.dates <- as.Date(c("1992-06-01", "1992-06-24", "1992-06-30", "1993-06-22", 
"1994-06-07", "1995-06-08"))
my.xts <- xts(1:6, my.dates)
start(my.xts)  # "1992-06-24"
end(my.xts)  # "1995-06-08"
apply.monthly(my.xts, mean)
#           [,1]
# 1995-06-08 3.5

The endpoints it chooses are based on looking at the month (June) alone.  I was 
able to get a value for each (month, year) in the timeseries with the following 
use of aggregate():

my.months <- months(my.dates)
my.years <- years(my.dates)
df1 <- data.frame(x = coredata(my.xts), dates = my.dates, months = my.months, 
years = my.years)
df2 <- aggregate(df1[-c(3,4)], df1[c("months", "years")], mean)
xts(df2$x, df2$dates)
#            [,1]
# 1992-06-18    2
# 1993-06-22    4
# 1994-06-07    5
# 1995-06-08    6

Two questions:  
1) Is there a more elegant way to do this? 
2) Shouldn't the xts documentation discuss the problem of sparse data?

Regards,
Scott Waichler
Pacific Northwest National Laboratory
Richland, WA  USA

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Reply via email to