rmacey
rmacey

Reputation: 667

EOMONTH returning NA

In the code below, the first call to the EOMONTH function returns NA when I expect February 28, 2019. The second call works correctly. Both calls work correctly in Excel which this function is trying to replicate. Is this a bug or am I doing something wrong?

library(tidyquant)
sdt <- as.Date("2019-01-31")
EOMONTH(sdt, 1)
EOMONTH(sdt, 2)

I have version 1.0.4 of tidyquant and version 4.1.2 of R (Bird Hippie)

Upvotes: 0

Views: 149

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368629

Use a an earlier date, for example the first of the month, to avoid the trouble of getting a non-existing end-of-month date. The problem is related to date calculus and difference period 'plus one month' (implicitly getting the last of the month which may not exist when you start from the 31st) and 'plus 30 days'.

> EOMONTH(as.Date("2019-01-01"), 0:4)
[1] "2019-01-31" "2019-02-28" "2019-03-31" "2019-04-30" "2019-05-31"
> 

If you check help(EOMONTH) you will see similar usage in the provided examples.

Upvotes: 2

Related Questions