Reputation: 745
for (j in 1:2){ #first iteration
for (i in 1:2){ #second iteration
lst1 <- replicate(i, data.frame(
matrix(c(letters[1:2],
sample(c(rep(1,100),0), size = 1),
sample(c(rep(0,100),1), size = 1)), ncol = 2) ), simplify = FALSE)
}
return(lst1)
}
I would expect that lst1
would return from second loop (i) to go to next iteration of the first loop (j).
I know I would do something like this replicate(i*j,....)
to get all iterations but this solution doesn't meet my expectations.
output is list of 2
a, b
0, 1
a, b
0, 1
but I would expect list of 4 (two iterations with two steps)
Upvotes: 0
Views: 421
Reputation: 3212
return()
is only meant to be used inside a function, not in a loop. Are you looking for the control flows break
or next
?
From the documentation in R:
break
breaks out of a for, while or repeat loop; control is transferred to the first statement outside the inner-most loop.next
halts the processing of the current iteration and advances the looping index. Both break and next apply only to the innermost of nested loops.
Upvotes: 1