[Following on from a question I asked about row-wise iteration where the value of a variable at row n depends on its value at row n-1, asked and answered here]
What approach should I take to a recursive row-wise iteration problem where you have multiple columns you want to fill in, and they are interdependent on one another?
For example, say you have this tibble:
library(tidyverse)
x <- tibble(t = c(1:10),
a = c(seq(100, 140, 10), rep(NA_real_, 5)),
b = c(runif(5), rep(NA_real_, 5)),
c = c(runif(5), rep(NA_real_, 5)))
We want to complete the a, b, and c columns. Where they already have a value, those values should remain. For the rows with NAs, we want to fill them in as follows:
a = lag(a, 1) * (1 + growth)
b = a * lag(b, 1)
c = b * lag(a, 2)
I have a feeling purrr::pmap() is the right approach for this type of multi-column problem, but I cannot get it to do what I want in this case.