Last active
November 7, 2020 15:27
-
-
Save bwiernik/1c08156bedffb019658c4e584b348353 to your computer and use it in GitHub Desktop.
R function to complete lists and make missing elements explicitly 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
complete_list <- function(.list, .names) { | |
if (missing(.names)) return(.list) | |
.list[.names[! (.names %in% names(.list)) ]] <- NA | |
return(.list) | |
} | |
a <- list(b = 1, c = 2) | |
a <- complete_list(a, c("a", "b", "c")) | |
dplyr::coalesce(a$a, a$b, a$c, 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment