Dimitri Liakhovitski
2011-Mar-09 21:04 UTC
[R] Vector of weekly dates starting with a given date
Hello! I have a date (a Monday): date<-20081229 mydates<-as.Date(as.character(date),"%Y%m%d") What package would allow me to create a vector that starts with that date (mydates) and contains dates for 51 Mondays that follow it (so, basically, 51 dates separated by a week)? Thanks a lot for the pointer! -- Dimitri Liakhovitski Ninah Consulting www.ninah.com
Dimitri Liakhovitski
2011-Mar-09 21:13 UTC
[R] Vector of weekly dates starting with a given date
I realize I can do something like this: date<-20081229 mydates<-as.Date(as.character(date),"%Y%m%d") for(i in 1:52){ # mydates<-c(mydates,mydates[length(mydates)]+7) } I just thought maybe there is a more elegant way... Thank you! Dimitri On Wed, Mar 9, 2011 at 4:04 PM, Dimitri Liakhovitski <dimitri.liakhovitski at gmail.com> wrote:> Hello! > > I have a date (a Monday): > > date<-20081229 > mydates<-as.Date(as.character(date),"%Y%m%d") > > What package would allow me to create a vector that starts with that > date (mydates) and contains dates for 51 Mondays that follow it (so, > basically, 51 dates separated by a week)? > > Thanks a lot for the pointer! > > -- > Dimitri Liakhovitski > Ninah Consulting > www.ninah.com >-- Dimitri Liakhovitski Ninah Consulting www.ninah.com
Hadley Wickham
2011-Mar-09 21:21 UTC
[R] Vector of weekly dates starting with a given date
On Wed, Mar 9, 2011 at 3:04 PM, Dimitri Liakhovitski <dimitri.liakhovitski at gmail.com> wrote:> Hello! > > I have a date (a Monday): > > date<-20081229 > mydates<-as.Date(as.character(date),"%Y%m%d") > > What package would allow me to create a vector that starts with that > date (mydates) and contains dates for 51 Mondays that follow it (so, > basically, 51 dates separated by a week)?library(lubridate) mydates <- ymd(date) + weeks(0:51) Hadley -- Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University http://had.co.nz/
Gabor Grothendieck
2011-Mar-09 22:17 UTC
[R] Vector of weekly dates starting with a given date
On Wed, Mar 9, 2011 at 4:13 PM, Dimitri Liakhovitski <dimitri.liakhovitski at gmail.com> wrote:> I realize I can do something like this: > date<-20081229 > mydates<-as.Date(as.character(date),"%Y%m%d") > for(i in 1:52){ ?# > ?mydates<-c(mydates,mydates[length(mydates)]+7) > } > > I just thought maybe there is a more elegant way... > Thank you!Here are two approaches: as.Date("2008-12-29") + seq(0, length = 53, by = 7) seq(as.Date("2008-12-29"), length = 53, by = "week") -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com