Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected result from EOMONTH function #219

Open
rexmacey opened this issue Jun 12, 2022 · 0 comments
Open

Unexpected result from EOMONTH function #219

rexmacey opened this issue Jun 12, 2022 · 0 comments

Comments

@rexmacey
Copy link

rexmacey commented Jun 12, 2022

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))
}```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant