In this tutorial you will learn about the Relational operators (also known as comparison operators) in R and how to use them. Like most of the programming languages, R programming also has relational operators to check the relationship between two operands. If the relation is true, then it returns the logical value TRUE; if the relation is false, then it returns the logical value FALSE.
Relational Operators in R
Following symbols are used as relational operators in R programming language:
Operator Symbols | Relational Operation | Example |
---|---|---|
$==$ | Is Equal | $10 == 5$ returns FALSE |
$!=$ | Is Not equal | $10 != 5$ returns TRUE |
$<$ | Is Less than | $10 < 5$ returns FALSE |
$>$ | Is Greater than | $10 > 5$ returns TRUE |
$<=$ | Is Less than or equal | $10 <= 5$ returns FALSE |
$>=$ | Is Greater than or equal | $10 >= 5$ returns TRUE |
Note than the output of relational operation contains the boolean values (TRUE
or FALSE
) based on the relational operator.
Examples of Relational Operators
To understand the use of relational operators, let us discuss some examples.
Equal to
10 == 5
[1] FALSE
Above R command check whether 10 equal to 5. Since 10 is not equal to 5, R returns FALSE.
Not Equal to
10 != 5
[1] TRUE
Above R command check whether 10 is not equal to 5. Since 10 is not equal to 5, R returns TRUE.
Less Than
10 < 5
[1] FALSE
Above R command check whether 10 is less than 5. Since 10 is not less than 5, R returns FALSE.
Greater Than
10 > 5
[1] TRUE
Above R command check whether 10 is greater than 5. Since 10 is greater than 5, R returns TRUE.
Less Than or Equal
10 <= 5
[1] FALSE
Above R command check whether 10 less than or equal to 5. Since 10 is not less than or equal to 5, R returns FALSE.
Greater Than or Equal
10 >= 5
[1] TRUE
Above R command check whether 10 greater than or equal to 5. Since 10 is greater than or equal to 5, R returns TRUE.
Relational operations on Vector and scalar
Like arithmetic operators, relational operations are also vectorized. When vector is compared with scalar using relational operator, R will recycled the scalar element to the length of vector and then perform element-wise relational operations.
# Create a vector x
x <- c(12, 10, 8, 16, 6)
x < 8
[1] FALSE FALSE FALSE FALSE TRUE
x == 10
[1] FALSE TRUE FALSE FALSE FALSE
x <= 8
[1] FALSE FALSE TRUE FALSE TRUE
Note than in all the above R codes, the left side of relational operator is a vector and right side of is a scalar. In such a case R recycle the scalar to match the length of vector and then perform the element-wise relational operation.
Relational operators can also be used to extract the elements of a vector based on some relational expression.
Suppose we need to extract only those elements of x
which are less than 8. To do this we can use following R code:
x <- c(12, 10, 8, 16, 6)
x[x > 8]
[1] 12 10 16
Above code extract only those elements of x
which are greater than 8.
x[x != 10]
[1] 12 8 16 6
Above code extract only those elements of x
which are not equal to 10.
x[x <= 8]
[1] 8 6
Above code extract only those elements of x
which are less than or equal to 8.
Relational operations on Vectors of equal length
If the length of both the operand in relational expression is same, R will check the relation element-wise.
x <- c(12, 10, 8, 16, 6)
y <- c(10, 12, 8, 18, 6)
# Are x and y equal (element-wise)?
x == y
[1] FALSE FALSE TRUE FALSE TRUE
# Are x less than equal to y-2 (element-wise)?
x <= y - 2
[1] FALSE TRUE FALSE TRUE FALSE
Relational operations on Vectors of unequal length
Recycling rule also work while comparing a shorter vector with a longer vector. R will recycle the elements of shorter vector and then perform the relational operation element-wise.
x <- c(10, 20, 6, 25)
y <- c(4, 20, 8)
As y
is shorter than x
, R will recycled the elements of shorter vector y
as (4,20,8,4)
and then perform the relational operations element-wise.
# Are x and y equal (element-wise)?
x == y
Warning in x == y: longer object length is not a multiple of shorter object
length
[1] FALSE TRUE FALSE FALSE
# Are x less than y (element-wise)?
x < y
Warning in x < y: longer object length is not a multiple of shorter object
length
[1] FALSE FALSE TRUE FALSE
# Are x less than or equal to y (element-wise)?
x <= y
Warning in x <= y: longer object length is not a multiple of shorter object
length
[1] FALSE TRUE TRUE FALSE
# Are x not equal to y (element-wise)?
x != y
Warning in x != y: longer object length is not a multiple of shorter object
length
[1] TRUE FALSE TRUE TRUE
Note than the output of relational operation on vectors will be another vector of same length and will contain the boolean values (TRUE
or FALSE
) based on the element-wise comparison.
Endnote
In this tutorial you learned about relational operators in R and how R uses recycling rule while performing relational operations.
To learn more about other operators in R, please refer to the following tutorials:
Assignment operators in R
Arithmetic operators in R
Logical operators in R
Miscellaneous operators in R
Precendence of Operators in R
Operators in R
Hopefully you enjoyed learning this tutorial on relational operators in R. Hope the content is more than sufficient to understand relational operators in R.