tidyverse / funs

Collection of low-level functions for working with vctrs
Other
34 stars 7 forks source link

Implement na_if() #43

Open hadley opened 4 years ago

hadley commented 4 years ago

Needs to be implemented in such a way that you can replace NaN values:

dplyr::na_if(c(NA, NaN, 1), 1)
#> [1]  NA NaN  NA

(from https://github.com/tidyverse/dplyr/issues/4627)

hadley commented 3 years ago

We need to figure out the semantics — I think probably it should using matching not equality. And maybe it should only be vectorised in its first argument? Maybe it should just be this?

na_if <- function(x, na) {
 x[vec_in(x, na)] <- NA
 x
}

Which makes it a special case of case_in(): case_in(x, y ~ NA)

DavisVaughan commented 2 years ago

Regarding na_if() being match-like, we document that na_if() is like SQL NULLIF(), and that uses equality. And a few people use it that way (after looking at GitHub). So it is probably best to leave it like this and promote some other way to do match-like replacement with NAs. See my full analysis in: https://github.com/tidyverse/dplyr/pull/6329#issue-1302637632