[R] sum of certain length

Gabor Grothendieck ggrothendieck at gmail.com
Mon May 24 07:38:01 CEST 2010


If the days are consecutive with no missing rows then the dates don't
need to be calculated and it could be represented as a ts series with
a frequency of 7.  Just aggregate it down to a frequency of 1:

   rain <- ts(dat$rain, freq = 7)
   aggregate(rain, 1)

If there are missing rows (or even there are none missing) you could
use zoo.  This time lets use dates:

   library(zoo)
   rain <- with(dat, zoo(rain, as.Date(paste(year, month, day, sep = "-"))))
   week <- 7 * (as.numeric(time(rain)-start(rain)) %/% 7) + start(rain) + 6
   aggregate(rain, week)

Each point in the aggregated series is associated with the date of the
end of its week.


On Sun, May 23, 2010 at 8:09 PM, Roslina Zakaria <zroslina at yahoo.com> wrote:
> Hi r-users,
>
> I have this data below.  I would like to obtain the weekly rainfall sum.  That is I would like to find sum for day 1 to day 7, day 8 - day15, and so on.
>    year month day rain
> 1  1922     1   1  0.0
> 2  1922     1   2  0.0
> 3  1922     1   3  0.0
> 4  1922     1   4  0.0
> 5  1922     1   5  0.0
> 6  1922     1   6  0.0
> 7  1922     1   7  0.0
> 8  1922     1   8  6.6
> 9  1922     1   9  1.5
> 10 1922     1  10  0.0
> 11 1922     1  11  0.0
> 12 1922     1  12  4.8
> 13 1922     1  13 14.7
> 14 1922     1  14  0.0
> 15 1922     1  15  0.0
> 16 1922     1  16  0.0
> 17 1922     1  17  0.0
> 18 1922     1  18  0.0
> 19 1922     1  19  0.0
> 20 1922     1  20  0.8
> 21 1922     1  21  0.0
> 22 1922     1  22  0.0
> 23 1922     1  23  0.0
> 24 1922     1  24  0.0
> 25 1922     1  25  0.0
> 26 1922     1  26  0.0
> 27 1922     1  27  0.0
> 28 1922     1  28  0.0
> 29 1922     1  29  0.0
> 30 1922     1  30  0.0
> 31 1922     1  31  0.0
> 32 1922     2   1  0.0
> 33 1922     2   2  0.0
> 34 1922     2   3  0.0
> 35 1922     2   4  0.0
> 36 1922     2   5  0.0
> 37 1922     2   6  0.0
> 38 1922     2   7  0.0
> 39 1922     2   8  0.0
> 40 1922     2   9  0.0
>
> Thank you.
>
>
>
>        [[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