I created the following loop that generates 1000 random numbers - and then repeats this process 10 times:
results <- list()
for (i in 1:10){
a = rnorm(1000,10,1)
b = rnorm(1000,10,1)
d_i = data.frame(a,b)
d_i$index = 1:nrow(d_i)
d_i$iteration = as.factor(i)
results[[i]] <- d_i
}
results_df <- do.call(rbind.data.frame, results)
Question: I would like to change this loop such that instead of only generating 1000 random numbers, it keeps generating random numbers until a certain condition is met, for example: KEEP generating random numbers UNTIL d_i$a > 10 AND d_i$b > 10.
Using a "WHILE()" statement, I tried to do this:
results <- list()
for (i in 1:10){
while (d_i$a > 10 & d_i$b >10) {
a = rnorm(1000,10,1)
b = rnorm(1000,10,1)
d_i = data.frame(a,b)
d_i$index = 1:nrow(d_i)
d_i$iteration = as.factor(i)
results[[i]] <- d_i
}
}
results_df <- do.call(rbind.data.frame, results)
Problem: However, this returns the following warnings (10 times):
Warning messages:
1: In while (d_i$a > 10 & d_i$b > 10) { :
the condition has length > 1 and only the first element will be used
And produces an empty table:
> results_df
data frame with 0 columns and 0 rows
Can someone please help me fix this problem?
Thanks!