Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date sequence with negative by

Tags:

date

r

How do I get a sequence of monthly dates that ends on a given month and has a given length? seq(as.Date(*), length, by="month") assumes the start date is given, not the end date, and AFAIK it's impossible to specify a negative value for by in this case.

ETA: that is, I want a sequence that spans a given period, but one whose end point is specified rather than the start point. So, something like seq(to="2000-03-01", len=3, by="month") --> 2000-01-01, 2000-02-01, 2000-03-01.

like image 479
Hong Ooi Avatar asked Aug 10 '11 02:08

Hong Ooi


2 Answers

Try this:

rev(seq(as.Date("2000-03-01"), length = 3, by = "-1 month"))
## [1] "2000-01-01" "2000-02-01" "2000-03-01"
like image 194
G. Grothendieck Avatar answered Oct 06 '22 17:10

G. Grothendieck


library(lubridate)
ymd('2011-03-03') - months(0:5)
like image 45
hadley Avatar answered Oct 06 '22 18:10

hadley