-
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
case_when should return an error with zero length RHS values #4170
Comments
The current behavior of Can you please simplify the reprex to use constants instead of complicated expressions? |
Here is a minimal reprex of what I believe is the issue library(dplyr, warn.conflicts = FALSE)
packageVersion("dplyr")
#> [1] '0.8.0.9000'
xx <- c("a")
dplyr::case_when(
xx == "a" ~ "A",
xx == "b" ~ character(0)
)
#> character(0) Created on 2019-02-17 by the reprex package (v0.2.1) Should it be here an error or |
Thanks, Christophe. I still think this is consistent with the recycling rules, but I agree it's confusing. We accept vectors on each LHS and RHS, and will recycle length-1 vectors to a common length, if possible. In your example, 0 is the only length different from 1, and this is what is used for the result: library(dplyr, warn.conflicts = FALSE)
my_recode <- function(xx, yy) {
dplyr::case_when(
xx == "a" ~ "A",
xx == "b" ~ yy
)
}
my_recode("a", "B")
#> [1] "A"
my_recode("a", LETTERS)
#> [1] "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A"
#> [18] "A" "A" "A" "A" "A" "A" "A" "A" "A"
my_recode("a", character(0))
#> character(0)
my_recode(letters, character(0))
#> Error: `xx == "b" ~ yy` must be length 26 or one, not 0
#> Backtrace:
#> █
#> 1. └─global::my_recode(letters, character(0))
#> 2. └─dplyr::case_when(xx == "a" ~ "A", xx == "b" ~ yy)
#> 3. └─dplyr:::validate_case_when_length(query, value, fs) /home/kirill/git/R/dplyr/R/case_when.R:160:2
#> 4. └─dplyr:::bad_calls(...) /home/kirill/git/R/dplyr/R/case_when.R:230:2
#> 5. └─dplyr:::glubort(fmt_calls(calls), ..., .envir = .envir) /home/kirill/git/R/dplyr/R/error.R:29:2 Created on 2019-02-17 by the reprex package (v0.2.1.9000) |
Ok it is clearer. I believe this is the part of the documentation that explains it:
But yes, it is a bit confusing to get Maybe some improvement of documentation is needed ?
|
We'll handle this centrally for the tidyverse: tidyverse/design#13 |
This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/ |
With this three examples of
case_when
utitlisation, I exepected to see examples 1 and 3 behavior but not the second oneI think the problem is related to issue #3246 since
str_c(character(0), collpase = ";")
returnscharacter(0)
whenpaste(character(0), collpase = ";")
return""
. Maybe it should be useful for users ofcase_when
that the function returns an error when RHS values have zero length.The text was updated successfully, but these errors were encountered: