given NA values fill them with the next non-na value

fill_backward(x)

Arguments

x

A numeric vector of values

Details

Works very well in context of dplyr to carry out backwards imputation

Examples

fill_backward(c(1.0, NA, 2))
#> [1] 1 2 2
fill_backward(c(NA, 1, NA, 2))
#> [1] 1 1 2 2
library(dplyr) df <- data_frame(id = c(1, 1, 2, 2), obs = c(1.2, 4.8, 2.5, NA))
#> Warning: `data_frame()` is deprecated as of tibble 1.1.0. #> Please use `tibble()` instead. #> This warning is displayed once every 8 hours. #> Call `lifecycle::last_warnings()` to see where this warning was generated.
df %>% group_by(id) %>% mutate(obs_imp = fill_backward(obs))
#> # A tibble: 4 x 3 #> # Groups: id [2] #> id obs obs_imp #> <dbl> <dbl> <dbl> #> 1 1 1.2 1.2 #> 2 1 4.8 4.8 #> 3 2 2.5 2.5 #> 4 2 NA NA