Skip to content

Instantly share code, notes, and snippets.

@dantonnoriega
Created September 2, 2021 19:02
Show Gist options
  • Save dantonnoriega/79c3ea021595962c39e3f76884760ca8 to your computer and use it in GitHub Desktop.
Save dantonnoriega/79c3ea021595962c39e3f76884760ca8 to your computer and use it in GitHub Desktop.
base R way to carry forward data
carryforward <- function(x, blank = is.na) {
# SOURCE: https://stackoverflow.com/a/32536507/3987905
# Find the values
if (is.function(blank)) {
isnotblank <- !blank(x)
} else {
isnotblank <- x != blank
}
# Fill down
x[which(isnotblank)][cumsum(isnotblank)]
}
dat <- as.data.frame(lapply(dat, carryforward))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment