I am trying to create a new column called g_it in a grouped data frame where the first value for each group will be the initial value in the column called exp and the subsequent values are (1 - 0.1) * lag(g_it) + exp.
I believe purrr:accumulate is what I'm looking for but I'm not sure how to set it up.
My data is:
structure(list(group = c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
4, 4, 4, 4), exp = c(0.493735461892577, 0.501836433242221, 0.4916437138759,
0.515952808021378, 0.503295077718154, 0.49179531615882, 0.504874290524285,
0.507383247051292, 0.505757813516535, 0.496946116128436, 0.515117811684508,
0.503898432364114, 0.493787594194582, 0.477853001128225, 0.511249309181431,
0.499550663909848)), class = "data.frame", row.names = c(NA,
-16L))
Expected output:
group exp g_it
1 0.4937355 0.4937355
1 0.5018364 0.94619835
1 0.4916437 1.343222215
1 0.5159528 1.724852794
2 0.5032951 0.5032951
2 0.4917953 0.94476089
2 0.5048743 1.355159101
2 0.5073832 1.727026391
3 0.5057578 0.5057578
3 0.4969461 0.95212812
3 0.5151178 1.372033108
3 0.5038984 1.738728197
4 0.4937876 0.4937876
4 0.477853 0.92226184
4 0.5112493 1.341284956
4 0.4995507 1.70670716