I need to make a calculation, based on the result of the previous calculation, and stop until a condition is met.
In the example below, I got nn0
, nn1
, nn2
, nn3
and nn4
from the previous results including t
, t1
, t2
, t3
and t4
.
At nn2
, the result stabilized. So, I would like to obtain a function or a while loop (I’m not sure yet), to do this iteration until it stabilizes at the smallest result, that is, in this case at nn2
.
n = 10
t = qt(1-0.05/2, df= n-1)
nn0 <- ceiling((t*5)/5)
#nn0 = 3
t1 = qt(1-0.05/2, df= ceiling(nn0)-1)
nn1 <- ceiling((t1*5)/5)
#nn1 = 5
t2 = qt(1-0.05/2, df= ceiling(nn1)-1)
nn2 <- ceiling((t2*5)/5)
#nn2 = 3
t3 = qt(1-0.05/2, df= ceiling(nn2)-1)
nn3 <- ceiling((t3*5)/5)
#nn3 = 5
t4 = qt(1-0.05/2, df= ceiling(nn3)-1)
nn4 <- ceiling((t4*5)/5)
#nn4 = 3
What I tried, without success:
n = 10
t = qt(1-0.05/2, df= n-1)
nn <- (t*5)/5
new_nn <- nn
# using while loop
while (new_nn < nn){
new_t = qt(1-0.05/2, df= ceiling(new_nn)-1)
new_nn <- (new_t*5)/5
}
Igor Cobelo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.