R/na_locf.R

Defines functions na_locf

Documented in na_locf

#' Fill in NAs with the previous non-missing value
#'
#' Inspired by zoo::na.locf0
#'
#' @param object an object
#' @param fromLast logical. Causes observations to be carried backward rather
#'   than forward. Default is FALSE.
#'
#' @export
#' @return Original object with NAs filled in
na_locf <- function(object, fromLast = FALSE) {
  if (fromLast) object <- rev(object)
  for (i in seq_along(object)) {
    if (i > 1 && is.na(object[i])) {
      object[i] <- object[i-1]
    }
  }
  if (fromLast) object <- rev(object)
  return(object)
}

Try the PKPDsim package in your browser

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

PKPDsim documentation built on March 7, 2023, 5:40 p.m.