Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R using lubridate subtract 6 months but the dates dose not exist?

I want to subtract 6 months from a date but using lubridate I get some NA’s. Here are my attempts:

> as.Date("2013-12-31") - months(6)
[1] NA
> as.Date("2013-12-30") - months(6)
[1] "2013-06-30"
> as.Date("2014-01-01") - months(6)
[1] "2013-07-01"
> as.Date("2012-12-31") - months(6)
[1] NA
> as.Date("2014-12-31") - months(6)
[1] NA
> 

is it because there is no "2013-06-31", June having only 30 days? In which case I might just subtract 366/2 = 183 days.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use the %m-% operator:

as.Date("2013-12-31") %m-% months(6)
#[1] "2013-06-30"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading