Skip to content

Instantly share code, notes, and snippets.

@bwiernik
Last active November 7, 2020 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bwiernik/1c08156bedffb019658c4e584b348353 to your computer and use it in GitHub Desktop.
Save bwiernik/1c08156bedffb019658c4e584b348353 to your computer and use it in GitHub Desktop.
R function to complete lists and make missing elements explicitly NA
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