R if else Multiple Conditions

What is the syntax to perform if else with multiple conditions in R? In R Programming, you can perform if else with multiple conditions either by using if…else statement or ifelse() function. And, you would need to use logical operators & for AND, | for OR.

Related: R ifelse() Function Usage with Examples

1. Quick Examples of if else Multiple Conditions

The following are quick examples of how to perform if else with multiple conditions.


# multiple conditions with & (AND)
a=50
if( a >= 40 & b <= 60 ){
  print('a value is in between 40 and 60')
}

# Multiple conditions with | (OR) 
str = 'HELLO'
if(str == "HELLO" | str == "hello"){
  print("str value is hello")
}

# Using ifelse() statement
a=50
val = ifelse(a >= 40 & b <= 60, 'TRUE', "FALSE")
print(val)

# Using multiple conditions on DataFrame
df$size <- ifelse(df$chapters > 0 & df$chapters < 20, 'SMALL','BIG')

2. R if else Multiple Conditions with And (&)

R uses & as local AND that would be used to check if else statement with multiple conditions.

The logical AND (&) operator will be TRUE if and only if all the operands are TRUE. Otherwise, it will be FALSE. It evaluates operads from left to right and returns immediately when the first condition or operand becomes FALSE.


# Multiple conditions with and
a=50
if( a >= 40 & b <= 60 ){
  print('a value is in between 40 and 60')
}

# Output
#[1] "a value is in between 40 and 60"

3. R if else Multiple Conditions with Or (|)

R uses | as local OR that would be used to check if else statement with multiple conditions.

The logical OR (|) operator will be TRUE if any operands are TRUE. if all operands FALSE, it will be FALSE. It evaluates operads from left to right and returns immediately when the first condition or operand becomes TRUE.


# Multiple conditions with or 
str = 'HELLO'
if(str == "HELLO" | str == "hello"){
  print("str value is hello")
}

# Output
#[1] "str value is hello"

4. Using ifelse() Function

R Base also has a function ifelse() that can be used similarly to if…else statement. This function takes three parameters. First the single or multiple conditions, second the value to be returned when the condition evaluates TRUE and third the value to return when the condition evaluates FALSE.


# Using ifelse() statement
a=50
val = ifelse(a >= 40 & b <= 60, 'TRUE', "FALSE")
print(val)

# Output
#[1] "TRUE"

5. Multiple Conditions with DataFrame

The following example shows that how to add a new column size to the DataFrame by using if else with multiple conditions in R. Here, we use dollar ($) in R to refer to DataFrame columns.


# Create dataframe
df=data.frame(id=c(11,22,33,44,55),
                        pages=c(32,45,33,22,56),
                        name=c("spark","python","R","java","jsp"),
                        chapters=c(76,86,11,15,7),
                        price=c(144,553,321,567,890))

#display the dataframe
print(df)

# Using multiple conditions
df$size <- ifelse(df$chapters > 0 & df$chapters < 20, 'SMALL','BIG')

Yields below output.

r if else multiple conditions

Conclusion

To perform multiple conditions in R you should use logical operators with in if…else statement or iflese() functions. The logical operators are & for AND, | for OR. The logical AND (&) operator will be TRUE if and only if all the operands are TRUE. Otherwise, it will be FALSE. It evaluates operads from left to right and returns immediately when the first condition or operand becomes FALSE. The logical OR (|) operator will be TRUE if any operands are TRUE. if all operands FALSE, it will be FALSE. It evaluates operads from left to right and returns immediately when the first condition or operand becomes TRUE.

Related Articles

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply