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 by using the if…else statement or the 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 performing 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 logical 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 & a <= 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 statements 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 the if…else statement. This function takes three parameters. The first one represents the condition of single/multiple, the second one represents the value to be returned when the condition evaluates TRUE, and the third one represents the value to be returned when the condition evaluates FALSE.


# Using ifelse() statement
a=50
val = ifelse(a >= 40 & a <= 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 within if…else statement or the iflese() functions. The logical operators are & for AND, 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

Naveen (NNK)

Naveen (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ @ LinkedIn

Leave a Reply