How could this code be altered to be applied at a group level within a dataframe, whilst iterating over the same columns? Haven't been able to repoduce with dplyr.
for (i in 2:nrow(df)) {
df$sd_x[i] <- 1 /
((1 / (df$sd_x[i-1] ^ 2)) +
(1 / (df$prior_sd[i] ^ 2))) ^ 0.5
}
group prior_sd sd_x
1 A 1.14 0.808
2 B 1.14 0.233
3 C 1.14 0.136
4 D 1.14 0.100
5 A 1.14 0.659
6 B 1.14 0.224
7 C 1.14 0.132
8 D 1.14 0.0994
9 A 1.14 0.571
10 B 1.14 0.212
When using dplyr, I don't believe you can reference the column you are creating with a lag of itself. The above loop code functions, however it is ignoring the grouping variable on the left (group), which is my dilemma.
Thanks