As you know, in logical operators in R, we have boolean operators: AND, OR, NOT, etc., but today we will learn the OR operator together. Let’s get started.
OR operator in R
The OR operator in R is used to compare two variables and return TRUE if one of the conditions is TRUE; if both are FALSE, it will return FALSE. See the table below for a better understanding.
Condition 1 | Condition 2 | OR |
TRUE | TRUE | TRUE |
TRUE | FALSE | TRUE |
FALSE | TRUE | TRUE |
FALSE | FALSE | FALSE |
Syntax
We can use the OR operator with | or || symbols in R and syntax as follows:
# Method 1: condition1 | condition2 #Method 2: condition1 || condition2
Parameters:
- condition1, condition2:
TRUE
orFALSE
is the result of the comparison.
Return: The value is TRUE
or FALSE
Example
Here, we will begin with the example check the numbers less than 10 and greater than 20.
# Initial value num1 = 5 num2 = 15 # Check condition is divisible and print print(paste("Check num1: ",num1<10 | num1>20)) print(paste("Check num2: ",num2<10 | num2>20))
Output
[1] "Check num1: TRUE"
[1] "Check num2: FALSE"
Now, we will build a program to print the one of the numbers divisible by 2 and greater than 0 with input is two numbers using OR operator.
# Initial value num1 = 5 num2 = 8 # Check condition is divisible and print if(num1%%2 == 0 | num2%%2 == 0) { print("One of the number or both is divisible by 2") } else { print("None of the number or both is divisible by 2") } # Check condition greater than 0 and print if(num1>0 | num2>0) { print("One of the number or both is greater than 0") } else { print("None of the number or both is greater than 0") }
Output
[1] "One of the number or both is divisible by 2"
[1] "One of the number or both is greater than 0"
Another example, we will work with a data frame.
# Initial a data frame dataFrame <- data.frame( names = c("Pauline GreenTara", "Chelsea Smith", "Rowena Richardson", "Hollie Davies", "Lexi Miller", "Christopher Moore"), math = c(8,6,4,2,5,2), physic = c(9,7,5,3,1,10), chemistry = c(6,7,9,4,3,1) ) # View data frame dataFrame attach(dataFrame) # Filter rows where math >= 5 or physic >= 5 dataFrame[(math>=5) | (physic>=5),] # Filter rows where math >= 6 or chemistry >= 5 dataFrame[(math>=7) | (chemistry>=8),]
Output
> # View data frame
> dataFrame
names math physic chemistry
1 Pauline GreenTara 8 9 6
2 Chelsea Smith 6 7 7
3 Rowena Richardson 4 5 9
4 Hollie Davies 2 3 4
5 Lexi Miller 5 1 3
6 Christopher Moore 2 10 1
> # Filter rows where math >= 5 or physic >= 5
> dataFrame[(math>=5) | (physic>=5),]
names math physic chemistry
1 Pauline GreenTara 8 9 6
2 Chelsea Smith 6 7 7
3 Rowena Richardson 4 5 9
5 Lexi Miller 5 1 3
6 Christopher Moore 2 10 1
>
> # Filter rows where math >= 6 or chemistry >= 5
> dataFrame[(math>=7) | (chemistry>=8),]
names math physic chemistry
1 Pauline GreenTara 8 9 6
3 Rowena Richardson 4 5 9
Summary
Above is what I can share with you. If you have any questions about OR operator in R, please leave a comment below. Good luck!
Maybe you are interested:
- The Symbols In R: How To Use & Code Examples
- Names in R: How To Use The Names() Function in R?
- Funs In R: What Does Funs Mean In R And Code Examples

Hi, guys! My name’s Scott Miller. My current job is a software developer and I have shared a lot of quality articles about Javascript, C, C++, C#, Python, PHP, R, Java programming languages. I’m hoping they can assist you.
Name of the university: HCMUS
Major: IT
Programming Languages: C, C++, Python, R, Java, JavaScript