Fills missing values in selected columns using the next or previous entry.

fill(.data, ..., direction = "down")

shift_fill(x, direction = "down")

Arguments

.data

A data.table

...

A selection of columns.

direction

Direction in which to fill missing values. Currently either "down" (the default), "up".

x

A vector.

Value

A filled data.table

Details

fill is filling data.table's columns, shift_fill is filling any vectors.

Examples


df <- data.table(Month = 1:12, Year = c(2000, rep(NA, 10),2001))
df
#>     Month  Year
#>     <int> <num>
#>  1:     1  2000
#>  2:     2    NA
#>  3:     3    NA
#>  4:     4    NA
#>  5:     5    NA
#>  6:     6    NA
#>  7:     7    NA
#>  8:     8    NA
#>  9:     9    NA
#> 10:    10    NA
#> 11:    11    NA
#> 12:    12  2001
df %>% fill(Year)
#>     Month  Year
#>     <int> <num>
#>  1:     1  2000
#>  2:     2  2000
#>  3:     3  2000
#>  4:     4  2000
#>  5:     5  2000
#>  6:     6  2000
#>  7:     7  2000
#>  8:     8  2000
#>  9:     9  2000
#> 10:    10  2000
#> 11:    11  2000
#> 12:    12  2001

df <- data.table(Month = 1:12, Year = c(2000, rep(NA, 10),2001))
df %>% fill(Year,direction = "up")
#>     Month  Year
#>     <int> <num>
#>  1:     1  2000
#>  2:     2  2001
#>  3:     3  2001
#>  4:     4  2001
#>  5:     5  2001
#>  6:     6  2001
#>  7:     7  2001
#>  8:     8  2001
#>  9:     9  2001
#> 10:    10  2001
#> 11:    11  2001
#> 12:    12  2001