lubridate::as_date() inside dplyr::


I am facing a weird situation with lubridate::as_date() inside a mutate Dplyr function.
I am trying to change dates from an Excel format to a date format.
The format I have is "45369", and I am trying to change the today column to "yyyy-mm-dd".
Taking into consideration the column can be any column in the data, the user will pick the name of the column through use input and be saved as [date_dc <- "user_input"].
Now, trying this format:
raw.main <- raw.main %>%
dplyr::mutate_at(vars(date_dc), ~ifelse(, NA,lubridate::as_date(as.numeric(.), origin = "1899-12-30")))
return 19800 in all the column
Through a direct assignment, it returns:
raw.main[[date_dc]] <- lubridate::as_date(as.numeric(raw.main[[date_dc]]), origin = "1899-12-30")
Any idea what is the issue here?
Sorry, was trying to upload more images, but I am a new user and posit didn't let me.

xx <- readxl::read_excel("test_raw_main.xlsx")
y <- function(raw.main, date_dc) {
  raw.main[[date_dc]] <- lubridate::as_date(as.numeric(raw.main[[date_dc]]), origin = "1899-12-30")
y2 <- function(raw.main, date_dc){
  raw.main <- raw.main %>%
    mutate(!!rlang::sym(date_dc) := ifelse(,NA,lubridate::as_date(as.numeric(.), origin = "1899-12-30")))
test1 <- y(xx, "today")
test2 <- y2(xx,"today")
y3 <- function(raw.main, date_dc){
  raw.main <- raw.main %>%
    mutate(!!rlang::sym(date_dc) := if_else(!!rlang::sym(date_dc)),
                                           lubridate::as_date(as.numeric(!!rlang::sym(date_dc)), origin = "1899-12-30")))
test3 <- y3(xx, "today")
all.equal(test1$today, test3$today)
y4 <- function(raw.main, date_dc){
  raw.main[[date_dc]] <- ifelse([[date_dc]]),NA,lubridate::as_date(as.numeric(raw.main[[date_dc]]), origin = "1899-12-30"))
test4 <- y4(xx, "today")

Just to add, we found that the issue was coming from ifelse (base).
Using the dplyr::if_else was fine

Hi Abraham,

Thanks OK, and it's actually better not to post images, but post the data you're working with instead, between a pair of triple backticks, like this:

``` r
<-- paste output of dput(head(raw.main)) here

So do you still have an issue, or have you figured it out?

I guess we figured it out, but was wondering why ifelse (base) was throwing this error.

Could you share the data you were using, as I suggested here?

I am scared the data I am working with I cannot share.
However, here is a quick mock data for the sake of the test.

mock_data <- data.frame(start = c(45369,45369,45369,45369),
                        end = c(45369,45369,45369,45369),
                        today = c(45369,45369,45369,45369))

## test_v1
test_v1 <- mock_data %>%
  mutate(today = ifelse(, NA, lubridate::as_date(today, origin = "1899-12-30")))


## test_v2
test_v2 <- mock_data %>%
  mutate(today = if_else(, NA, lubridate::as_date(today, origin = "1899-12-30")))


That's fine — sharing mock data that replicates the issue is actually better since it can help to highlight both misunderstanding and what might be going wrong. Thanks for sharing it.

It turns out that ifelse() strips the true and false vectors of their attributes, so that in the case of dates, we see the equivalent of this:

lubridate::as_date("1899-12-30") |> 
  print() |> 
#> [1] "1899-12-30"
#> [1] -25569

Created on 2024-05-06 with reprex v2.0.2