[R] aggredating date data

Gabor Grothendieck ggrothendieck at gmail.com
Thu Jan 13 06:52:37 CET 2011


On Wed, Jan 12, 2011 at 7:35 PM, Gene Leynes <gleynes+r at gmail.com> wrote:
> I like the zoo package, and there are several helpful examples.
> library(zoo)
>
> You can easily convert your data into a zoo object using
> I was actually just doing this using this function:
> LoadReturnData=function(x){
>    ret = read.csv(x)
>    ret = zoo(ret[ , -1], as.Date(ret[ , 1]))
>    colnames(ret) = toupper(colnames(ret))
>    return(ret)
> }
> fnd = LoadReturnData('/Data/SomeSpecialData.csv')
>
> My data is already in weeks, and aggregating to months is easy using
> as.yearmon
>
> MonthIndex=as.yearmon(index(fnd))
> aggregate(.~MonthIndex, data=fnd, sum)
>
> If you have daily data and you need weeks, then you'll have to create a
> vector to indicate the week, like the MonthIndex above.
>
> e.g. for 365 days
> WeekIndex = rep(1:53, each=7, length.out=365)
>

Also note the existence of read.zoo and its aggregate= argument.

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list