Unexpected result from EOMONTH function
rexmacey opened this issue · 0 comments
rexmacey commented
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.
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)
(update) In StackOverflow a thread indicated that one should adjust the start date to an earlier date. I propose a different logic based on the fact that the end of a month is one day before the first day of the next month. Here's some code.
if (rlang::is_missing(start_date))
start_date <- TODAY()
start_date <- lubridate::as_date(start_date)
y <- year(start_date) + floor(months / 12)
months <- months - floor(months / 12) * 12
m <- month(start_date) + months
if(m >= 11) y <- y + 1
m <- m + 1
if(m > 12) m <- m - 12
return(make_date(y, m, 1) - days(1))
}```