# 如何跳过和中断R中的循环

##### 问题描述投票：-2回答：3

``````      validData <-  function(GM, GSD,sampleSize, p) {

count=0
for (i in 1:n) {
repeat {
lod <- quantile(rlnorm(1000000, log(GM), log(GSD)), p = p)
X_before <- rlnorm(sampleSize, log(GM), log(GSD))
Xs <-  ifelse(X_before <= lod, lod,  X_before)
delta <- ifelse(X_before <= lod, 1,  0)
pct_cens <- sum(delta)/length(delta)
print(pct_cens)
if (pct_cens == 0 & pct_cens ==1) next

else {

sumStats <- summary(Xs)

Med <- sumStats[3]
Ave <- sumStats[4]

}

count<- count+1

if (count == 10) break
}}
return(c(pct_cens, Med, Ave))
}

validData(GM=1,GSD=2,sampleSize=10,p=0.1)
``````
r
##### 3个回答
1

``` dset <- function (GM, GSD, n, p) { Mean <- array() Median <- array() count = 0 while(count < 10) { lod <- quantile(rlnorm(1000000, log(GM), log(GSD)), p = p) X_before <- rlnorm(n, log(GM), log(GSD)) Xs <- ifelse(X_before <= lod, lod, X_before) delta <- ifelse(X_before <= lod, 1, 0) pct_cens <- sum(delta)/length(delta) print(pct_cens) if (pct_cens == 0 | pct_cens == 1 ) next else {count <- count +1 if (pct_cens > 0 & pct_cens < 1) { sumStats <- summary(Xs) Median[count] <- sumStats[3] Mean [count]<- sumStats[4] print(list(pct_cens=pct_cens,Xs=Xs, delta=delta, Median=Median,Mean=Mean)) } } } return(data.frame( Mean=Mean, Median=Median)) } ```

0

```validData <- function(GM, GSD,Size, p) { for (i in 1:20) { count <- 1 repeat { lod <- quantile(rlnorm(1000000, log(GM), log(GSD)), p = p) X_before <- rlnorm(Size, log(GM), log(GSD)) Xs <- ifelse(X_before <= lod, lod, X_before) delta <- ifelse(X_before <= lod, 1, 0) pct_cens <- sum(delta)/length(delta) if (pct_cens == 0 & pct_cens ==1) function() #your foo goes here else { mles <- find_MLE(c(0,0), Xs, delta) GM_est <- mles[1] GSD_est <- mles[2] AM_est <- exp(log(GM_est) + 1 ) SD_est<- sqrt((AM_est)^2*exp(log(GSD_est)^2)) D95th_est <- GM_est*(GSD_est^1.645) } } return(c(GM_est,GSD_est,AM_est,SD_est,D95th_est)) count<- count+1 if (count == 10) break }} ```

0

```# OUTER LOOP for(i in 1:2) { print(paste("Outer loop iteration", i)) # INNER LOOP (will run max 10 times) for(j in 1:10) { print(paste("Inner loop iteration", j)) if (runif(1) > 0.4) { # Randomly break the inner loop print(paste("Breaking inner loop", j)) break() } } } ```

`如果要跳到外循环发生错误（而不是基于条件时），请参阅here] >>`