Lubridate: managing leap year date

Morning,

i've problem in substracting years for leap year date.
R give me NA if i substract or add 1 years to 2020 - february -29th.

Example:


library("lubridate")
# subsctrac year to leap year date
ymd('2020-02-29') - years(1)
# or
interval(ymd('2017-01-02'), ymd('2020-02-29') - years(1))

the only solutions is using the function for imaginary date:


library("lubridate")

ymd('2020-02-29') %m-% years(1)

?

thanks in advance
bye
MC

Hi @martinocrippa,

What do you mean by "imaginary"? I think 29-02-2020 is a an actual (even though in the future) date.

Lubridate official cheatsheet wrote “imaginary”(in attach a screenshot), i think someone decide to call the function with that name because if substract one year to 29-02-2020 in can find a date, because 2019 is not a leap year.

I see - thanks for clarifying, but isn't that the behaviour you are looking for?

Lubridate Developer write to me on github repository issue:

Yes. Unfortunately the original design of period arithmetics is not that most people expect. So your only option for now is to use %m+% or add_with_rollback functions. I will likely be changing the internals of lubridate by the end of the year and there might be a global user option to configure this behavior.

( github issue )

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.