chessosapiens
chessosapiens

Reputation: 3419

how to skip executing several lines with if clause in R

how to skip executing several lines of codes if a condition in IF statement is met. Condition happens occasionally so whenever it happens we need to skip executing several lines of codes, for example:

 if ( op=='A) {

     #skip doing everything here }
{

  #some line of codes which will be run in any condition

or is it possible to do it using while or for loops ?

Upvotes: 1

Views: 4001

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 389215

You could test the condition using

if (op != 'A') {
    #Code1
    #Code2
    #Don't execute this part for op == 'A' 
}

#Code3
#Code4
#Execute this part for everything

Upvotes: 2

nibble
nibble

Reputation: 404

You can use the next keyword. For example, the code below will not print the values in the vector x = 1:10 from 5 to 8:


    x = 1:10
    for(i in x){ 
        if(i>=5 && i<=8){
            next #Skips printing when i=5,6,7 and 8
        }
        print(i) #Code you don't want skipped goes here
    }





Upvotes: 4

Related Questions