R/filldown.R

Defines functions filldown

Documented in filldown

filldown <- function(data, cols, by = NULL) {
  df <- bt_as_data_frame(data)
  cols <- bt_resolve_cols(df, cols)
  by <- if (is.null(by)) character(0) else bt_resolve_cols(df, by)

  if (length(cols) < 1L) {
    stop("`cols` must contain at least one column.", call. = FALSE)
  }

  if (length(by) == 0L) {
    for (nm in cols) df[[nm]] <- bt_locf(df[[nm]])
  } else {
    groups <- bt_group_rows(bt_engine_groups(df, by)$id)
    for (nm in cols) {
      for (idx in groups) df[[nm]][idx] <- bt_locf(df[[nm]][idx])
    }
  }

  bt_as_data_table(df)
}

Try the basetable package in your browser

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

basetable documentation built on Sept. 18, 2026, 9:13 a.m.