Hi there,
I can get the for-loop to work, I can get the while loop to work. But I
can't get a for loop to work nested within the while loop - why?
Please help,
Teresa
# Why doesn't this nested loop work?
n.max <- 300
NUM <- 25
n.sim <- 10
j <- (n.max/NUM)*n.sim
results <- matrix(0, nrow=j, ncol=2)
while(NUM <= n.max){
for(i in 1:n.sim){
k <- (NUM/25)*i
results[k,1] <- k
results[k,2] <- NUM
}
NUM <- NUM + 25
}
results
#### TRY WHILE LOOP ONLY
results <- matrix(0, nrow=12, ncol=2)
n.max <- 300
NUM <- 25
while(NUM <= n.max){
k <- NUM/25
results[k,1] <- k
results[k,2] <- NUM
NUM <- NUM + 25
}
results
# It works, here are the results
# [,1] [,2]
# [1,] 1 25
# [2,] 2 50
# [3,] 3 75
# [4,] 4 100
# [5,] 5 125
# [6,] 6 150
# [7,] 7 175
# [8,] 8 200
# [9,] 9 225
#[10,] 10 250
#[11,] 11 275
#[12,] 12 300
### Try For Loop Only
n.sim <- 10
NUM <- 25
results <- matrix(0, nrow=10, ncol=2)
for(i in 1:n.sim){
results[i,1] <- i
results[i,2] <- NUM
}
results
# it works, here are the results
# [,1] [,2]
# [1,] 1 25
# [2,] 2 25
# [3,] 3 25
# [4,] 4 25
# [5,] 5 25
# [6,] 6 25
# [7,] 7 25
# [8,] 8 25
# [9,] 9 25
#[10,] 10 25
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html