Skip to content

Instantly share code, notes, and snippets.

@MyKo101
Created March 15, 2021 09:15
Show Gist options
  • Select an option

  • Save MyKo101/cbeb878b99da773260ddbc8ba2c60877 to your computer and use it in GitHub Desktop.

Select an option

Save MyKo101/cbeb878b99da773260ddbc8ba2c60877 to your computer and use it in GitHub Desktop.
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