How to account for leap years?

前端 未结 4 359
陌清茗
陌清茗 2020-12-06 09:58

I have some doubts about the leap years, how can I be sure that by using a formula like this

add.years= function(x,y){    
if(!isTRUE(all.equal(y,round(y))))         


        
4条回答
  •  独厮守ぢ
    2020-12-06 10:30

    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.

提交回复
热议问题