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

if_else missing argument does not work when using %in% to evaluate. #7054

Closed
Raesu opened this issue Jul 16, 2024 · 3 comments
Closed

if_else missing argument does not work when using %in% to evaluate. #7054

Raesu opened this issue Jul 16, 2024 · 3 comments

Comments

@Raesu
Copy link

Raesu commented Jul 16, 2024

This function could refer to the missing argument to fill in missing values. It works except for when %in% is used in the condition to check.


x <- c(NA, 1,2,3)

# Expected behavior, NA passes through
if_else(x == 2, TRUE, FALSE)

# NA is converted to FALSE
if_else(x %in% c(2), TRUE, FALSE)

# NA is converted to FALSE, `missing` argument has no impact
if_else(x %in% c(2), TRUE, FALSE, missing=NA)
@DavisVaughan
Copy link
Member

The conversion of the NA to FALSE happens with %in%, and is unrelated to dplyr code

x <- c(NA, 1,2,3)

x %in% 2
#> [1] FALSE FALSE  TRUE FALSE

So there is nothing for dplyr to do here. Converting NA to FALSE is just a known side effect of how %in% and match() work.

@DavisVaughan
Copy link
Member

DavisVaughan commented Jul 16, 2024

If you really want NAs to pass through, you can use vctrs::vec_in() with na_equal = FALSE

vctrs::vec_in(c(NA, 1, 2, 3), 2, na_equal = FALSE)
#> [1]    NA FALSE  TRUE FALSE

But for the most part that is considered "advanced usage"

@Raesu
Copy link
Author

Raesu commented Jul 16, 2024

Helpful, I did not know that. Thanks.

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

2 participants