Created
February 7, 2021 20:20
-
-
Save MyKo101/434a091dc6ec576de7f97e8eac9f0643 to your computer and use it in GitHub Desktop.
try_na
This file contains 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
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