Compare in R

Find this useful? Support us: Star on GitHub 6
Category: String | Language: R

In R, we can compare two numbers using comparison operators. There are six comparison operators in R as listed below:

1. > (greater than): It returns TRUE if the left operand is greater than the right operand, otherwise FALSE.

2. < (less than): It returns TRUE if the left operand is less than the right operand, otherwise FALSE.

3. >= (greater than or equal to): It returns TRUE if the left operand is greater than or equal to the right operand, otherwise FALSE.

4. <= (less than or equal to): It returns TRUE if the left operand is less than or equal to the right operand, otherwise FALSE.

5. == (equal to): It returns TRUE if the two operands are equal, otherwise FALSE.

6. != (not equal to): It returns TRUE if the two operands are not equal, otherwise FALSE.

Here are some examples for comparing numbers using these operators in R:

# Greater than operator
3 > 2    # TRUE
5 > 7    # FALSE

# Less than operator
2 < 3 # TRUE
6 < 4 # FALSE

# Greater than or equal to operator
4 >= 4 # TRUE
5 >= 7 # FALSE

# Less than or equal to operator
9 <= 9 # TRUE
8 <= 5 # FALSE

# Equal to operator
2 == 2 # TRUE
6 == 3 # FALSE

# Not equal to operator
3 != 4 # TRUE
8 != 8 # FALSE

These comparison operators can be used in combination with conditional statements and loops to build more complex programs in R.