I am trying to write a nested loop code to simulate 10 columns of data in a data frame with 101 rows. The first row of data has been assigned as starting values. Each column should be different as my matrix r is generated from random normals; however, the resulting values in each column are exactly the same. To give some context for the looping indices:
tmax=100; ncol(pop_sims) = 12 (so a total of 10 iterations, 3-12); ncol(r) = 10
for (i in 1:tmax){
for (j in 3:ncol(pop_sims)){
for(k in 1:ncol(r)){
if (pop_sims[i,j]*exp(r[i,k]) <2) {
pop_sims[i+1,j]<- 0}
else {
pop_sims[i+1,j] <- pop_sims[i,j]*exp(r[i,k])}
}}}
Any thoughts would be appreciated.
UPDATE: Instead of working with the multiple loops, I omitted the use of the matrix r and simplified my loops.
for (i in 1:tmax){
for (j in 1:10){
if (pop_sims[i,j]*exp(r[i,j]) <2) {
pop_sims[i+1,j]<- 0}
else {
pop_sims[i+1,j] <- pop_sims[i,j]*exp(rnorm(1,mean=0.02, sd=0.1))}
}}