Skip to content

Instantly share code, notes, and snippets.

@MyKo101
Created February 7, 2021 20:20
Show Gist options
  • Save MyKo101/434a091dc6ec576de7f97e8eac9f0643 to your computer and use it in GitHub Desktop.
Save MyKo101/434a091dc6ec576de7f97e8eac9f0643 to your computer and use it in GitHub Desktop.
try_na
try_na <- function(...){
f_list <- purrr::map(list(...),rlang::as_function)
f_len <- length(f_list)
function(.x) {
out <- rep(NA,length(.x))
i <- 1
while(any(is.na(out)) & i <= f_len){
out[is.na(out)] <- f_list[[i]](.x[is.na(out)])
i <- i + 1
}
out
}
}
fun <- try_na(~log(.x),~log(-.x))
fun(-10:10)
my_date_parser <- try_na(
~readr::parse_date(.,format="%d-%b-%y",locale=locale("fr")),
~as.numeric(.)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment