Maximilian
Maximilian

Reputation: 4229

Working with multiple if else statements

I cannot figure out what I need. Here is simplification what I actually need.

I would like to process each function if certain number appears in a vector, here is example:

v <- c(111,88,222,99,555,1,9,6)

if(111 %in% v){
   x <- 111+0.1
 } else if(222 %in% v){
   y <- 222+0.1
 } else if(555 %in% v){
   z <- 555+0.1
 }

I would like to process each function if given number is found in vector v.

In the above example the if else example would give out number 111.1,222.1,333.1, what I'm doing wrong here?

Basicaly, I would like to calculate each function if certain number appears in vector.

Upvotes: 2

Views: 66

Answers (1)

ShellFish
ShellFish

Reputation: 4551

You want the if check to always be evaluated but once the first one is true, the following can never be checked because they are preceded by an èlse. Just drop the else clauses:

v <- c(111,88,222,99,555,1,9,6)

if(111 %in% v){
   x <- 111+0.1
}
if(222 %in% v){
   y <- 222+0.1
}
if(555 %in% v){
   z <- 555+0.1
}

Upvotes: 2

Related Questions