2017-10-04 2 views
0

Ist es möglich, mehrere if-Anweisungen innerhalb einer while-Schleife zusammenzufassen?Verschachteln mehrerer if-Anweisungen innerhalb einer while-Schleife in R

Ich versuche nur ein einfaches Beispiel zu erstellen, mich um sie zu entlarven:

i <- 1 
while(i <=10) { 
if(i > 6){ 
cat("i=",i,"and is bigger than 6.\n") 
}else{if(3<i & i<6){ 
cat("i=",i,"and is between 3 and 6.\n") 
}else{ 
cat("i=",i,"and is 3 or less.\n") 
} 
i<-i+1 
cat("At the bottom of the loop i is now =",i,"\n") 
} 

Mein Beispielcode hält bei i = 7 stecken zu bleiben und will für immer laufen. Wie kann ich das vermeiden?

+0

Sie haben Ihre Antwort - es gibt zu viele 'waren {' s. Ich würde hinzufügen, dass Sie gut beraten sind, mehr Sorgfalt mit der Formatierung Ihres Codes zu verwenden. Verwenden Sie Leerzeichen und Einzüge richtig, und Sie werden viel weniger wahrscheinlich auf solche Probleme stoßen. – dww

+0

Wären Sie bereit, den obigen Code richtig zu formatieren, damit ich weiß, was Sie meinen? Oder ein Beispiel geben? Das obige Format folgt dem Ansatz meines Professors, aber ich würde gerne Ihre klarere Methode kennen. – Jeremy

+0

Hadleys Style Guide ist hier: http://adv-r.had.co.nz/Style.html oder der Google Style Guide ist hier https://google.github.io/styleguide/Rguide.xml. Wähle einen aus und bleib dabei. – dww

Antwort

0

Wie @ Alex P erwähnt haben Sie eine zusätzliche {.

immer Sie auch Ihre else if nur um zu überprüfen, ob i vereinfachen können, ist größer als gleich 3 (Sie wissen schon i geringer sein wird als oder gleich 6 von dem ersten if Zustand versagt, wo Sie, wenn i > 6 überprüfen):

i <- 1 
while(i <=10) { 
    if(i > 6) { 
     cat("i =", i, "and is bigger than 6.\n") 
    } else if(i >= 3) { 
     cat("i =", i ,"and is between 3 and 6 inclusive.\n") 
    } else { 
     cat("i =", i ,"and is less than 3.\n") 
    } 
    i = i + 1 
    cat("At the bottom of the loop i is now =", i ,"\n") 
} 

Ausgang:

i = 1 and is less than 3. 
At the bottom of the loop i is now = 2 
i = 2 and is less than 3. 
At the bottom of the loop i is now = 3 
i = 3 and is between 3 and 6 inclusive. 
At the bottom of the loop i is now = 4 
i = 4 and is between 3 and 6 inclusive. 
At the bottom of the loop i is now = 5 
i = 5 and is between 3 and 6 inclusive. 
At the bottom of the loop i is now = 6 
i = 6 and is between 3 and 6 inclusive. 
At the bottom of the loop i is now = 7 
i = 7 and is bigger than 6. 
At the bottom of the loop i is now = 8 
i = 8 and is bigger than 6. 
At the bottom of the loop i is now = 9 
i = 9 and is bigger than 6. 
At the bottom of the loop i is now = 10 
i = 10 and is bigger than 6. 
At the bottom of the loop i is now = 11 
1

Sie hatten ein extra { nach dem ersten else

i <- 1 
while(i <=10) { 
    if(i > 6){ 
    cat("i=",i,"and is bigger than 6.\n") 
    }else if(3<i & i<6){ 
    cat("i=",i,"and is between 3 and 6.\n") 
    }else{ 
    cat("i=",i,"and is 3 or less.\n") 
    } 
    i<-i+1 
    cat("At the bottom of the loop i is now =",i,"\n") 
} 
Verwandte Themen