[R] Help with date specification

Gabor Grothendieck ggrothendieck at gmail.com
Thu Sep 17 18:59:55 CEST 2009


Try either of these which convert to year and qtr and then
shift the qtr by one.  The resulting series is labeled by
the year in which the series dates start.  Use
as.integer(...) + 1 if you prefer to label by ending year.

library(zoo)
# test data
DF <- data.frame(date = Sys.Date() + 1:1000, value = 1:1000)

# 1. Using a data frame --
#     uses as.yearqtr from zoo but no zoo objects

tapply(DF$value, as.integer(as.yearqtr(DF$date) - 0.25), sum)

# 2. or converting the time series to zoo and then aggregating

z <- zoo(DF$value, as.Date(DF$date))
aggregate(z, as.integer(as.yearqtr(time(z))-0.25), sum)

In future please provide some of your data using dput (see ?dput)
to make it easier to answer and code if relevant.

On Thu, Sep 17, 2009 at 12:11 PM, Subodh Acharya <shoebodh at gmail.com> wrote:
> Hi everyone,I have a data daily data (x) for 10 years starting from
> 04-01-1995 to 03-31-2005.
> I was able to get the yearly sum for the ten years using
> aggregate(x, years, sum).
> But this gave me the yearly sum for 1995 (Apr- Dec); 1996 (Jan-Dec)
> ---------2005 (Jan-Mar).
> But I want to get the aggregates for Apr-1995 to Mar 1996, Apr 1996- mar
> 1997 and so on.
>
> your help will be higly appreciated.
> Thanks in advance
>
> --
> Acharya, Subodh
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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