Confusing dplyr::if_else error "`false` must be a `Date` object, not a `Date` object"

Hi, manipulating a dataframe imported by haven::read_sas, the call of dplyr::il_else led to the confusing following error message : error false must be a Date object, not a Date object

Both columns I used in the true/false arguments of if_else were of the class Date :

class(test$datePourrie)
[1] "Date"
class(test$dateLiq)
[1] "Date"

but the type of the columns was different :

typeof(test$dateLiq)
[1] "integer"
typeof(test$datePourrie)
[1] "double"

leading to the confusing error message.

Maybe the blame is on haven::read_sas which should not create Date format with integer type, anyway it will be great if the dplyr::if_else error message said rather in this case : "error false must be of integer format, not a double format. It would have save me some time.

Regards,

Could you please turn this into a self-contained reprex (short for reproducible example)? It will help us help you if we can be sure we're all working with/looking at the same stuff.

install.packages("reprex")

If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page. The reprex dos and don'ts are also useful.

There's also a nice FAQ on how to do a minimal reprex for beginners, below:

What to do if you run into clipboard problems

If you run into problems with access to your clipboard, you can specify an outfile for the reprex, and then copy and paste the contents into the forum.

reprex::reprex(input = "fruits_stringdist.R", outfile = "fruits_stringdist.md")

For pointers specific to the community site, check out the reprex FAQ.

I'm sorry I don't think I can. I must have a ill-defined dataframe and I get an error trying to dpasta it

dpasta(da)
"Error in strrep(" ", char_length - nchar(char_vec)) :
invalid 'times' value"

Even trying the factor trick

da %>%

  • dplyr::mutate_if(is.factor, as.character) %>%
  • datapasta::tribble_paste()
    Error in strrep(" ", char_length - nchar(char_vec)) :
    invalid 'times' value

You should report unexpected behavior in the GitHub issue tracker for dplyr.

Reprex isolating the error:

library(dplyr)

int_date <- Sys.Date()
num_date <- Sys.Date()
storage.mode(int_date) <- "integer"

if_else(TRUE, int_date, num_date)
# Error: `false` must be a `Date` object, not a `Date` object
# Call `rlang::last_error()` to see a backtrace

If you need to know how to report bugs for any package, you can run this command:

# dplyr is the example, but any package name will do
packageDescription("dplyr", fields = c("BugReports", "Maintainer"))
# BugReports: https://github.com/tidyverse/dplyr/issues
# Maintainer: Hadley Wickham <hadley@rstudio.com>
# 
# -- File: C:/Users/nwerth/Documents/R/win-library/3.6/dplyr/Meta/package.rds 
# -- Fields read: BugReports, Maintainer

If there's a BugReports field, it tells you where to go. Otherwise, send an email to the person named in Maintainer.

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.