Fills missing values in the selected columns using the next or previous entry. Can be done by group.

Supports tidyselect

fill.(.df, ..., .direction = c("down", "up", "downup", "updown"), .by = NULL)

Arguments

.df

A data.frame or data.table

...

A selection of columns. tidyselect compatible.

.direction

Direction in which to fill missing values. Currently "down" (the default), "up", "downup" (first down then up), or "updown" (first up and then down)

.by

Columns to group by when filling should be done by group

Examples

df <- data.table(
  a = c(1, NA, 3, 4, 5),
  b = c(NA, 2, NA, NA, 5),
  groups = c("a", "a", "a", "b", "b")
)

df %>%
  fill(a, b)
#> # A tidytable: 5 × 3
#>       a     b groups
#>   <dbl> <dbl> <chr> 
#> 1     1    NA a     
#> 2     1     2 a     
#> 3     3     2 a     
#> 4     4     2 b     
#> 5     5     5 b     

df %>%
  fill(a, b, .by = groups)
#> # A tidytable: 5 × 3
#>       a     b groups
#>   <dbl> <dbl> <chr> 
#> 1     1    NA a     
#> 2     1     2 a     
#> 3     3     2 a     
#> 4     4    NA b     
#> 5     5     5 b     

df %>%
  fill(a, b, .direction = "downup", .by = groups)
#> # A tidytable: 5 × 3
#>       a     b groups
#>   <dbl> <dbl> <chr> 
#> 1     1     2 a     
#> 2     1     2 a     
#> 3     3     2 a     
#> 4     4     5 b     
#> 5     5     5 b