Skip to contents

The function will loop through a vector and will substitute NA values with the value it last encountered or replaced.

Usage

fill(x, forward = TRUE, inc = 0)

Arguments

x

(vector) Vector to be filled.

forward

(logical) Should the loop go forward or backward?

inc

(numeric) Only if x is numeric, the function will increase the substituted value by this amount (useful for filling in sequences).

Value

A logical vector.

Details

NAs won't be substituted when they are the first values the loop encounters.

Examples

# forward, replace with previous
dummy<- c(TRUE, FALSE, NA, TRUE, FALSE, NA)
fill(dummy)
#> [1]  TRUE FALSE FALSE  TRUE FALSE FALSE

# forward, replace with previous+1
dummy2 <- c(1,NA, 3, 1, 2, NA, NA, 9, NA,3)
fill(dummy2, inc=1)
#>  [1]  1  2  3  1  2  3  4  9 10  3

# backward, replace with previous in loop direction
fill(dummy2, inc=0, forward=FALSE)
#>  [1] 1 3 3 1 2 9 9 9 3 3