-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
More informative error for case_when()
failing due to not matching NA
#6206
Comments
case_when
failing due to not matching NAcase_when()
failing due to not matching NA
Here is a related confusing error message with x=1:10
dplyr::case_when(
x < 5 ~ 1,
TRUE ~ NA
)
#> Error in names(message) <- `*vtmp*`: 'names' attribute [1] must be the same length as the vector [0]
dplyr::case_when(
x < 5 ~ 1,
TRUE ~ NA_real_
)
#> [1] 1 1 1 1 NA NA NA NA NA NA Created on 2022-04-04 by the reprex package (v2.0.1) The error message is different but I's think the internal problem is the same. Not sure if it will be corrected by #6225 though. |
Here's a reprex for the two examples above using dplyr installed from #6225 library(dplyr, warn.conflicts = FALSE)
tibble(a = c("yes", "no"),
b = as.integer(c(0,2))) %>%
mutate(b = case_when(a == "yes" ~ NA_real_,
TRUE ~ b))
#> Error in `mutate()`:
#> ! Problem while computing `b = case_when(a == "yes" ~ NA_real_, TRUE ~
#> b)`.
#> Caused by error in `case_when()`:
#> ! `b` must be a double vector, not an integer vector. Created on 2022-04-04 by the reprex package (v2.0.1) x=1:10
dplyr::case_when(
x < 5 ~ 1,
TRUE ~ NA
)
#> Error in `dplyr::case_when()`:
#> ! `NA` must be a double vector, not a logical vector. Created on 2022-04-04 by the reprex package (v2.0.1) |
It seems that since recently mutate
case_when()
calls are more strictly evaluated with regard to the variable type (or it always was as strict and some other R or package update, now produces integer instead of double values).That's fine, but I think, ideally the error message would be more informative than
'names' attribute [1] must be the same length as the vector [0]
, especially for this borderline case where (depending on the data) the same code might produce either integers or doubles.Created on 2022-03-04 by the reprex package (v2.0.1)
Session info
The text was updated successfully, but these errors were encountered: