Skip to content

Instantly share code, notes, and snippets.

@joshbode
Last active December 14, 2015 18:49
Show Gist options
  • Select an option

  • Save joshbode/5132005 to your computer and use it in GitHub Desktop.

Select an option

Save joshbode/5132005 to your computer and use it in GitHub Desktop.
Default missing values progressively in R
# coalesce missing values
# e.g.
# > x = c(NA, 2, NA, 4)
# > y = c(1, NA, NA, 4)
# > z = c(1, NA, 3, 4)
# > coalesce(x, y, z)
coalesce = function(x, ...) {
result = x
for (y in list(...)) {
mask = is.na(result)
result = replace(result, mask, y[mask])
}
return(result)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment