Lubridate: managing leap year date


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.


# 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:


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


thanks in advance

1 Like

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.

1 Like

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 )

1 Like

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