Comparison or relational operators in R are used to compare variables with values.
The comparison operator comes in between the left and right variables or operands. It tells us the relationship between the two.
Comparison operators in R include:
==
!=
>
<
>=
<=
The table below contains all the comparison operators in R. Let’s say x
and y
hold the values of 1
and 2
, respectively:
Comparison Operator | Description | Example |
---|---|---|
== |
Equal to operator. This returns true only if the two operands are equal |
x==y is not true because the value of x is not equal to y . |
!= |
Not equal to operator. This returns true if the two operands are not equal |
x!=y is true because the value of x is not equal to the value of y |
> |
Greater than operator. This returns true only if the value of the left operand is greater than the right operand |
x>y is not true because the value of x is not greater than y |
< |
Less than operator. This returns true only if the value of the left operand is greater than the right operand |
x<y is true because the value of x is less than the value of y |
>= |
Greater than or equal to operator. This returns true only if the value of the left operand is either greater or equal to the right operand |
x>=y is not true because the value of x is neither greater nor equal to y |
<= |
Less than or equal to operator. This returns true only if the value of the left operand is either lesser or equal to the right operand |
x<=y is true because the value of x is less than the value of y |
==
)This returns true
only if the two operands are equal.
# creating our variablesx <- 2y <- 2# Using the eqaul to comparison operatorif (x == y){print('x is equal to y')}else{print('x is not equal to y')}
!=
)This returns true
if the two operands are not equal.
# creating our variablesx <- 1y <- 2# Using the not eqaul to comparison operatorif (x != y){print('x is not equal to y')}else{print('x is equxl to y')}
>
)This returns true
only if the value of the left operand is greater than the right operand.
# creating our variablesx <- 2y <- 1# Using the greater than comparison operatorif (x > y){print('x is greater than y')}else{print('x is not greater than y')}
<
)This returns true
only if the value of the left operand is less than the right operand.
# creating our variablesx <- 1y <- 2# Using the less than comparison operatorif (x < y){print('x is less than y')}else{print('x is not less than y')}
>=
)This returns true
only if the value of the left operand is either greater or equal to the right operand.
# creating our variablesx <- 7y <- 2# Using the greater than or equal to comparison operatorif (x >= y){print('x is greater than or equal to y')}else{print('x is not greater than or equal to y')}
<=
)This returns true
only if the value of the left operand is either less than or equal to the right operand.
# creating our variablesx <- 1y <- 2# Using the less than or equal to comparison operatorif (x <= y){print('x is lesser than or equal to y')}else{print('x is not lesser than or equal to y')}