[R] seq.dates in reverse?

Marc Schwartz marc_schwartz at me.com
Wed Jun 30 05:05:31 CEST 2010


On Jun 29, 2010, at 7:17 PM, Jonathan Greenberg wrote:

> Pardon the barrage of time series related questions, but another issue
> I'm trying to solve is how to determine a sequence of dates a la
> seq.dates() except going BACKWARDS in time, e.g. if seq.dates()
> allowed for the "to" variables to be set alone, rather than the from=.
> Ultimately, I'd like to have a set of dates preceding a given date in
> predefined intervals (the same ones seq.dates() uses would be fine).
> Thoughts?  Would there be an easy way to "reverse engineer" a starting
> date given the "by=" variable and the "length="?  With the exception
> of using "by=days", I'm a bit unfamiliar with how to easily determine
> what date was, say, 4 months ago without doing a lot of string hacking
> (seq.dates() conveniently keeps the days of the month constant when
> generate date sequences, which is what I'd like).
> 
> --j

Jonathon,

Do you mean something like:

> seq(as.Date("2010-07-29"), length = 2, by = "-4 months")
[1] "2010-07-29" "2010-03-29"

?

Note that the 'by' argument can be a negative interval. See the third bullet in the Details section of ?seq.Date.

HTH,

Marc Schwartz



More information about the R-help mailing list