Skip to contents

Wrapper for data.table::setnafill for fast filling by group and non-numeric columns. Displatches to setnafill_ functions based on column class and type of fill

Usage

setnafill(
  x,
  type = c("const", "locf", "nocb"),
  fill = NA,
  cols = seq_along(x),
  by = NA
)

setnafill_const_simple(x, type = "const", fill = NA, cols = seq_along(x))

setnafill_group(x, type = "locf", cols = seq_along(x), by = NA)

Arguments

x

data.table

type

character, one of "const", "locf" or "nocb". Defaults to "const".

fill

vector, value to be used to fill when type=="const".

cols

numeric or character vector specifying columns to be updated.

by

character string of columns to group by

Value

data.table, filled in-place

Functions

  • setnafill_const_simple(): Simple constant fill for factors and character

  • setnafill_group(): rolling join to quickly do setnafill by groups

Examples

x <- data.table::data.table(a = NA_character_, b = rep(seq(1, 2), each = 10))
x[1, a := "a"]
#>          a     b
#>     <char> <int>
#>  1:      a     1
#>  2:   <NA>     1
#>  3:   <NA>     1
#>  4:   <NA>     1
#>  5:   <NA>     1
#>  6:   <NA>     1
#>  7:   <NA>     1
#>  8:   <NA>     1
#>  9:   <NA>     1
#> 10:   <NA>     1
#> 11:   <NA>     2
#> 12:   <NA>     2
#> 13:   <NA>     2
#> 14:   <NA>     2
#> 15:   <NA>     2
#> 16:   <NA>     2
#> 17:   <NA>     2
#> 18:   <NA>     2
#> 19:   <NA>     2
#> 20:   <NA>     2
#>          a     b
x[10, a := "x"]
#>          a     b
#>     <char> <int>
#>  1:      a     1
#>  2:   <NA>     1
#>  3:   <NA>     1
#>  4:   <NA>     1
#>  5:   <NA>     1
#>  6:   <NA>     1
#>  7:   <NA>     1
#>  8:   <NA>     1
#>  9:   <NA>     1
#> 10:      x     1
#> 11:   <NA>     2
#> 12:   <NA>     2
#> 13:   <NA>     2
#> 14:   <NA>     2
#> 15:   <NA>     2
#> 16:   <NA>     2
#> 17:   <NA>     2
#> 18:   <NA>     2
#> 19:   <NA>     2
#> 20:   <NA>     2
#>          a     b

y <- data.table::copy(x)
setnafill(y, type = "const", fill = "test", cols = "a")[]
#>          a     b
#>     <char> <int>
#>  1:      a     1
#>  2:   test     1
#>  3:   test     1
#>  4:   test     1
#>  5:   test     1
#>  6:   test     1
#>  7:   test     1
#>  8:   test     1
#>  9:   test     1
#> 10:      x     1
#> 11:   test     2
#> 12:   test     2
#> 13:   test     2
#> 14:   test     2
#> 15:   test     2
#> 16:   test     2
#> 17:   test     2
#> 18:   test     2
#> 19:   test     2
#> 20:   test     2
#>          a     b

y <- data.table::copy(x)
setnafill(y, type = "locf", by = "b")[]
#>          a     b
#>     <char> <int>
#>  1:      a     1
#>  2:      a     1
#>  3:      a     1
#>  4:      a     1
#>  5:      a     1
#>  6:      a     1
#>  7:      a     1
#>  8:      a     1
#>  9:      a     1
#> 10:      x     1
#> 11:   <NA>     2
#> 12:   <NA>     2
#> 13:   <NA>     2
#> 14:   <NA>     2
#> 15:   <NA>     2
#> 16:   <NA>     2
#> 17:   <NA>     2
#> 18:   <NA>     2
#> 19:   <NA>     2
#> 20:   <NA>     2
#>          a     b

y <- data.table::copy(x)
setnafill(y, type = "nocb", by = "b")[]
#>          a     b
#>     <char> <int>
#>  1:      a     1
#>  2:      x     1
#>  3:      x     1
#>  4:      x     1
#>  5:      x     1
#>  6:      x     1
#>  7:      x     1
#>  8:      x     1
#>  9:      x     1
#> 10:      x     1
#> 11:   <NA>     2
#> 12:   <NA>     2
#> 13:   <NA>     2
#> 14:   <NA>     2
#> 15:   <NA>     2
#> 16:   <NA>     2
#> 17:   <NA>     2
#> 18:   <NA>     2
#> 19:   <NA>     2
#> 20:   <NA>     2
#>          a     b