0

Please do not close this question as it is not a duplicate of How to subtract months from a date in R? I am trying to convert a date of type IDate to mondate and then back to IDate.

In the code below I am trying to get a date 6 months prior to the given date date1, which is IDate object.

library(mondate)
date1 = as.IDate("2020-02-10")
date2 = as.IDate(mondate(date1)-6)

However, I am getting the warning

Warning message:
Attempting to convert class 'IDateDate' to 'mondate' via 'as.Date' then 'as.numeric'. Check results! 

Can someone tell me how to correct this warning?

Solution - As suggested by Ronak in comments - date1 %m-% months(6) works just fine with IDate class.

Saurabh
  • 1,566
  • 10
  • 23
  • 1
    With `lubridate` you can do `date1 %m-% months(6)`. There is also an option with `mondate` in linked post. – Ronak Shah Feb 24 '21 at 06:47
  • I am using the ```mondate``` library to get 6 months prior date and getting warning while converting it to ```IDate```. I could not find any help in the linked post on this issue. – Saurabh Feb 24 '21 at 06:59
  • ```date1 %m-% months(6)``` did worked out fine. Thanks! – Saurabh Feb 24 '21 at 07:14

1 Answers1

1

IDate isn't a very common type of Date class hence mondate returns you a warning to check the results and make sure it is correct since it has internally converted IDate to date class.

library(mondate)
mondate(date1) - 6

#mondate: timeunits="months"
#[1] 08/11/2019

Warning message: Attempting to convert class 'IDateDate' to 'mondate' via 'as.Date' then 'as.numeric'. Check results!

This is a friendly warning and can be ignored. If you don't want the warning convert to Date class explicitly.

mondate(as.Date(date1)) - 6
#mondate: timeunits="months"
#[1] 08/11/2019

Using lubridate's %m-% works fine with IDate as well as Date class.

library(lubridate)

date1 %m-% months(6)
#[1] "2019-08-10"
as.Date(date1) %m-% months(6)
#[1] "2019-08-10"
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213