How to subtract years?

gt6989b picture gt6989b · Jul 22, 2010 · Viewed 49.4k times · Source

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 as.Date('2010-03-17').

How would I do that?

Answer

hadley picture hadley · Jul 23, 2010

With lubridate

library(lubridate)
ymd("2010/03/17") - years(2)