You can check if a year is a leap year with leap_year
from lubridate
.
years <- 1895:2005
years[leap_year(years)]
This package will also handle not generating impossible 29ths of February.
ymd("2000-2-29") + years(1) # NA
ymd("2000-2-29") %m+% years(1) # "2001-02-28"
The %m+%
“add months” operator, as mentioned by @VitoshKa, rolls the date back to the end of the previous month if the actual day doesn’t exist.