Hello,

I'm working on a simulation of sorts and need to populate a data frame over a certain period. There are predefined probabilities which determine which values are computed in each cell based on the corresponding values in the previous row. I have a spreadsheet example where I have done this successfully and am now trying to implement in R.

My starting point would look something like this:

```
mydata <- tibble(
year = c(0, seq(1:60)),
event = c(0, rep(0.05, 12), rep(0.10, 12),
rep(0.15, 12), rep(0.20, 12), rep(0.25, 12)),
A = c(1000, rep(0, 60)),
B = c(rep(0, 61)),
C = c(rep(0, 61)),
D = c(rep(0, 61)),
E = c(rep(0, 61))
)
```

Where `year`

is equal to the year in the simulation and `event`

is equal to the probability that some event has happened.

For the purposes of this example, let's say that the formulas to populate each of A:D were as follows:

```
# Formula for B
mydata$B[[i-1]]*(1-0.04) + mydata$B[[i-1]]*(1-mydata$event[[i]])
# Formula for C
mydata$B[[i-1]]*(mydata$event[[i]])
# Formula for D
mydata$C[[i-1]]*(1-0.04) + mydata$D[[i-1]]*(1-mydata$event[[i]])
# Formula for E
mydata$A[[i-1]]*0.04 + mydata$B[[i-1]]*mydata$event[[i]] +
(mydata$C[[i-1]] + mydata$D[[i]])*mydata$event[[i]] + mydata$E[[i-1]]
```

The `A`

variable will be 0 for each year, except for year 0.

As you can see in the formulas, I need to populate the dataset by row and then by column. I know how to do the loop for the column, where I am seeking guidance is to get the formulas to run across rows first.

Any advice this community could offer would be appreciated!