Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

r
operator
communitycreator

How to use “&” and “|” to combine conditional statements in R

Onyejiaku Theophilus Chidalu

Overview

The & (and) and the | (or) operators are used to combine conditional statements in R.

Conditional statements in R programming are used to make decisions based on certain conditions.

In a simpler term, a conditional statement is a type of coding instruction used to compare values and expressions to make decisions.

Using the "|" operator

The "|" (or) operator can be used to combine a conditional statement.

Example

In the example below, we use the | operator to combine conditional statements that will test if a variable value is greater or not greater than the others and vice versa.

Code

# Creating our variables
x <- 200
y <- 33
z <- 500

# using the | operator to combine conditional statements
if (x > y | z > x){
  print("Both conditions are true")
}
Implementing the `OR` operator

Explanation

  • Lines 2 to 4: We create our variables, x, y, and z.
  • Line 7: We combine conditional statements if x > y and if z > x together using the | operator by simplifying it to become if (x > y | z > x).
  • Line 8: We print a statement, provided that the conditions given in line 7 are true.

When one of the conditions is False

What happens when one of the given conditional statement(s) is incorrect?

Let’s look at the code below, where the value of the x variable is greater than the y variable, but we state otherwise.

Code

# Creating our variables
x <- 200
y <- 33
z <- 500

# using the | operator to combine conditional statements
if (x < y | z > x){
  print("One of the conditions is true")
}
When one of the conditional statements is wrong using the "OR" operator

Explanation

In the code above, despite a wrong statement, x < y, the | operator executes the code because the other statement provided, z > x, is true. However, the program will not execute when the conditional statements provided are all false.

Using the "&" operator

The "&" (and) operator can be used to combine a conditional statement.

Example

In the example below, we use the & operator to combine conditional statements that will test if a variable value is greater or not greater than the others and vice versa.

Code

# Creating our variables
x <- 200
y <- 33
z <- 500

# using the & operator to combine conditional statements
if (x > y & z > x){
  print("Both conditions are true")
}
Implementing the "AND" operator

Explanation

  • Lines 2 to 4: We create our variables, x, y, and z.
  • Line 7: We combine conditional statements if x > y and if z > x together using the & operator by simplifying it to become if (x > y & z > x).
  • Line 8: We print a statement provided that the conditions given in line 7 are true.

When one of the conditions is False

What happens when one of the given conditional statement(s) is incorrect?

Let’s look at the code below where the value of thex variable is greater than the y variable, but we state it otherwise.

Code

# Creating our variables
x <- 200
y <- 33
z <- 500

# using the & operator to combine conditional statements
if (x < y & z > x){
  print("Both conditions are true")
}else{
  print("False condition(s) provided!")
}
When one of the conditional statements is wrong using the "and" operator

Explanation

In the code above, one of the provided conditional statements is wrong, and for that reason, the code cannot execute the command provided. This is not the same for the | (or) operator, in which if one condition is false and the other is true, the code will still execute.

Summary

  • For the code to execute using an | operator, at least one condition must be true.
  • For the code to execute using the & operator, the two conditions provided must be true.

RELATED TAGS

r
operator
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring