R/fill_down.R

Defines functions fill_down

Documented in fill_down

#' Fills NA values with previous non-NA value
#'
#' This is a base approximation of \code{tidyr::fill()}
#'
#' @param x a list having some number of non-NA values
#' @return a list where NA values have been replaced with the closest previous
#'   non-NA value
#'
#' @seealso \code{fill_down} is a helper function inside \code{lines_sort}

fill_down <- function(x) {
  if (length(x) > 1) {
    keep <- c(TRUE, !is.na(x[-1]))
    x[keep][cumsum(keep)]
  } else {
    x
  }

}

Try the SwimmeR package in your browser

Any scripts or data that you put into this service are public.

SwimmeR documentation built on March 31, 2023, 8:27 p.m.