I wrote the code below. The aim is to obtain positive numbers only. So, rnorm(5,2,2) can produce negative numbers too, but I want it to produce positive ones only. To do so, I used repeat in R, but it does not work properly. What can you suggest for fixing it? Here is the code:
for (i in 1:5){
repeat{
x <- rnorm(5,2,2)
if ((length(which(x<0)))==0){break}
print(x)
}
}
Your original code works. You’re probably getting confused by the print(x) which doesn’t represent the end result