I would use purrr::accumulate
in this case of a lag of one. It just require a small modification in your function to take two arguments : previous value and actual value.
See
library(dplyr)
#>
#> Attachement du package : 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(purrr)
x <- tibble(a = c(1:10),
b = c(seq(100, 140, 10), rep(NA_real_, 5)) )
fill_in <- function(prev, new, growth = 0.03) {
if_else(!is.na(new), new, prev * (1 + growth))
}
options(pillar.sigfig = 5)
x %>%
mutate(b = accumulate(b, fill_in))
#> # A tibble: 10 x 2
#> a b
#> <int> <dbl>
#> 1 1 100
#> 2 2 110
#> 3 3 120
#> 4 4 130
#> 5 5 140
#> 6 6 144.2
#> 7 7 148.53
#> 8 8 152.98
#> 9 9 157.57
#> 10 10 162.30
Created on 2019-08-31 by the reprex package (v0.3.0)