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

Would it be beneficial for na_if() to accept NaN as a y argument? #4627

Closed
hlynurhallgrims opened this issue Nov 22, 2019 · 4 comments
Closed
Labels
feature a feature request or enhancement funs 😆

Comments

@hlynurhallgrims
Copy link

hlynurhallgrims commented Nov 22, 2019

I got to thinking. I was using tidyr::fill(), expecting it to replace some NaNs in the same way as it does for NAs. Seeing that it doesn't and understanding that it's not desireable for it to do so I wanted to raise the question if dplyr::na_if() should perhaps be made a more convenient alternative to dplyr::if_else() when you want to explicitly turn NaNs to NAs.

In the cases where you'd want to carry forward the last observation before a NaN you could use na_if(x, y = NaN) before using tidyr::fill().

A reprex follows:

vector <- c(1:5, rep(NaN, 5))
vector
#>  [1]   1   2   3   4   5 NaN NaN NaN NaN NaN

#This changes nothing
dplyr::na_if(x = vector, y = NaN)
#>  [1]   1   2   3   4   5 NaN NaN NaN NaN NaN

# Perhaps this is desireable? Perhaps not?
na_if <- function(x, y) {
# check_length() function left out for convenience here

if (is.nan(y)) {
  x[is.nan(x)] <- NA
} else {
  x[x == y] <- NA
}
x
}

# Our updated na_if function:
na_if(x = vector, y = NaN)
#>  [1]  1  2  3  4  5 NA NA NA NA NA

Created on 2019-11-22 by the reprex package (v0.2.0).

@hadley
Copy link
Member

hadley commented Dec 10, 2019

Minimal reprex:

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

Created on 2019-12-10 by the reprex package (v0.3.0)

@hadley hadley added feature a feature request or enhancement funs 😆 labels Dec 10, 2019
@hadley
Copy link
Member

hadley commented Dec 11, 2019

Moving this issue to funs, which is where the fix will eventually happen.

@hadley hadley closed this as completed Dec 11, 2019
@lock
Copy link

lock bot commented Jun 24, 2020

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/

1 similar comment
@lock
Copy link

lock bot commented Jun 24, 2020

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/

@lock lock bot locked and limited conversation to collaborators Jun 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature a feature request or enhancement funs 😆
Projects
None yet
Development

No branches or pull requests

2 participants