You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 nothingdplyr::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 hereif (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
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/
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/
lockbot
locked and limited conversation to collaborators
Jun 24, 2020
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
I got to thinking. I was using
tidyr::fill()
, expecting it to replace someNaN
s in the same way as it does forNA
s. Seeing that it doesn't and understanding that it's not desireable for it to do so I wanted to raise the question ifdplyr::na_if()
should perhaps be made a more convenient alternative todplyr::if_else()
when you want to explicitly turnNaN
s toNA
s.In the cases where you'd want to carry forward the last observation before a
NaN
you could usena_if(x, y = NaN)
before usingtidyr::fill()
.A reprex follows:
Created on 2019-11-22 by the reprex package (v0.2.0).
The text was updated successfully, but these errors were encountered: