I am working with the R programming language. I got the following loop to run:
library(dplyr)
list_results <- list()
for (i in 1:100){
c1_i = c2_i = c3_i = 0
while(c1_i + c2_i + c3_i < 15 ){
num_1_i = sample_n(iris, 30)
num_2_i = sample_n(iris, 30)
num_3_i = sample_n(iris, 30)
c1_i = mean(num_1_i$Sepal.Length)
c2_i = mean(num_2_i$Sepal.Length)
c3_i = mean(num_3_i$Sepal.Length)
ctotal_i = c1_i + c2_i + c3_i
combined_i = rbind(num_1_i, num_2_i, num_3_i)
nrow_i = nrow(unique(combined_i[duplicated(combined_i), ]))
}
inter_results_i <- data.frame(i, c1_i, c2_i, c3_i, nrow_i, ctotal_i)
list_results[[i]] <- inter_results_i
}
Now, I want to try and add a second condition to this loop. Using this post as a reference (How to have two conditions in a While loop?), I tried to do this as follows:
list_results <- list()
for (i in 1:100){
c1_i = c2_i = c3_i = ctotal_i = 0
while(c1_i + c2_i + c3_i < 15 && nrow_i == 0 ) {
num_1_i = sample_n(iris, 30)
num_2_i = sample_n(iris, 30)
num_3_i = sample_n(iris, 30)
c1_i = mean(num_1_i$Sepal.Length)
c2_i = mean(num_2_i$Sepal.Length)
c3_i = mean(num_3_i$Sepal.Length)
ctotal_i = c1_i + c2_i + c3_i
combined_i = rbind(num_1_i, num_2_i, num_3_i)
nrow_i = nrow(unique(combined_i[duplicated(combined_i), ]))
}
inter_results_i <- data.frame(i, c1_i, c2_i, c3_i, ctotal_i, nrow_i)
list_results[[i]] <- inter_results_i
}
But for some reason, this is always producing an "empty" list.
Can someone please show me what I am doing wrong and how to fix this?
Thanks!