Created
March 15, 2021 09:15
-
-
Save MyKo101/cbeb878b99da773260ddbc8ba2c60877 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| na <- function (x) | |
| { | |
| x <- if (missing(x)) { | |
| prev_call <- sys.call(sys.nframe() - 2) | |
| if (identical(prev_call[[1]], quote(if_else)) | | |
| identical(prev_call[[1]], quote(dplyr::if_else))) { | |
| prev_env <- sys.frame(sys.nframe() - 1) | |
| prev_env$x | |
| } | |
| else logical(1) | |
| } | |
| else x | |
| y <- NA | |
| y <- type.convert(y, typeof(x)) | |
| class(y) <- class(x) | |
| rep(y, length(x)) | |
| } | |
| x <- 1:10 | |
| y <- letter[1:10] | |
| z <- rbinom(10,1,0.5) == 1 | |
| if_else(x < 5, x, na()) | |
| if_else(x > 5, y, na()) | |
| if_else(x > 5, z, na()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment