gt6989b gt6989b - 1 year ago 101
R Question

How to subtract years in R?

I have a date in R, e.g.

dt = as.Date('2010/03/17');

I would like to subtract 2 years from this date, without worrying about leap years and such issues, getting
. How would I do that? Thanks!

Answer Source

The easiest thing to do is to convert it into POSIXlt and subtract 2 from the years slot.

> d <- as.POSIXlt(as.Date('2010/03/17'))
> d$year <- d$year-2
> as.Date(d)
[1] "2008-03-17"

See this related question:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download