dt_fill: Fill with data.table

View source: R/fill.R

dt_fillR Documentation

Fill with data.table

Description

Fills in values, similar to tidyr::fill(), by within data.table. This function relies on the Rcpp functions that drive tidyr::fill() but applies them within data.table.

Usage

dt_fill(
  dt_,
  ...,
  id = NULL,
  .direction = c("down", "up", "downup", "updown"),
  immutable = TRUE
)

Arguments

dt_

the data table (or if not a data.table then it is coerced with as.data.table)

...

the columns to fill

id

the grouping variable(s) to fill within

.direction

either "down" or "up" (down fills values down, up fills values up), or "downup" (down first then up) or "updown" (up first then down)

immutable

If TRUE, dt_ is treated as immutable (it will not be modified in place). Alternatively, you can set immutable = FALSE to modify the input object.

Value

A data.table with listed columns having values filled in

Examples


set.seed(84322)
library(data.table)

x <- 1:10
dt <- data.table(
  v1 = x,
  v2 = shift(x),
  v3 = shift(x, -1L),
  v4 = sample(c(rep(NA, 10), x), 10),
  grp = sample(1:3, 10, replace = TRUE)
)
dt_fill(dt, v2, v3, v4, id = grp, .direction = "downup")
dt_fill(dt, v2, v3, v4, id = grp)
dt_fill(dt, .direction = "up")

TysonStanley/tidyfast documentation built on April 10, 2024, 9:20 a.m.