R/replace_na_with_lag.R

Defines functions replace_na_with_lag_df

Documented in replace_na_with_lag_df

#' Replace NA with lag (for all numeric columns in data)
#' @param data data.frame
#' 
replace_na_with_lag_df <- function(data) {
  
  num_col <- sapply(data, is.numeric)
  
  from_na_to_lag <- function(x) {
    
    x_lag <- dplyr::lag(x)
    
    x[is.na(x)] <- x_lag[is.na(x)]
    
    return(x)
    
  }
  
  data[, num_col] <- sapply(data[, num_col], from_na_to_lag)
  
  return(data)
  
}
kristian-bak/rumination documentation built on Oct. 31, 2022, 6:44 p.m.