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.

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

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)

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")
#> by v2 v3 v4 #> 1: 2 2 2 7 #> 2: 2 2 4 7 #> 3: 2 7 9 9 #> 4: 2 8 10 9 #> 5: 1 1 3 3 #> 6: 1 3 5 3 #> 7: 1 4 6 3 #> 8: 1 5 7 3 #> 9: 3 6 8 2 #> 10: 3 9 8 2
dt_fill(dt, v2, v3, v4, id = grp)
#> by v2 v3 v4 #> 1: 2 NA 2 NA #> 2: 2 2 4 7 #> 3: 2 7 9 9 #> 4: 2 8 10 9 #> 5: 1 1 3 NA #> 6: 1 3 5 3 #> 7: 1 4 6 3 #> 8: 1 5 7 3 #> 9: 3 6 8 2 #> 10: 3 9 8 2
dt_fill(dt, .direction = "up")
#> Null data.table (0 rows and 0 cols)