Trusted answers to developer questions

Onyejiaku Theophilus Chidalu

**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:

- Equal to
`==`

- Not equal to
`!=`

- Greater than
`>`

- Less than
`<`

- Greater than or equal to
`>=`

- Less than or equal to
`<=`

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 variables x <- 2 y <- 2 # Using the eqaul to comparison operator if (x == y){ print('x is equal to y') }else{ print('x is not equal to y') }

Implementing the equal to operator

`!=`

)This returns `true`

if the two operands are not equal.

# creating our variables x <- 1 y <- 2 # Using the not eqaul to comparison operator if (x != y){ print('x is not equal to y') }else{ print('x is equxl to y') }

Implementing the not equal to operator

`>`

)This returns `true`

only if the value of the left operand is greater than the right operand.

# creating our variables x <- 2 y <- 1 # Using the greater than comparison operator if (x > y){ print('x is greater than y') }else{ print('x is not greater than y') }

Implementing the greater than operator

`<`

)This returns `true`

only if the value of the left operand is less than the right operand.

# creating our variables x <- 1 y <- 2 # Using the less than comparison operator if (x < y){ print('x is less than y') }else{ print('x is not less than y') }

Implementing the less than operator

`>=`

)This returns `true`

only if the value of the left operand is either greater or equal to the right operand.

# creating our variables x <- 7 y <- 2 # Using the greater than or equal to comparison operator if (x >= y){ print('x is greater than or equal to y') }else{ print('x is not greater than or equal to y') }

Implementing the greater than or equal to operator

`<=`

)This returns `true`

only if the value of the left operand is either less than or equal to the right operand.

# creating our variables x <- 1 y <- 2 # Using the less than or equal to comparison operator if (x <= y){ print('x is lesser than or equal to y') }else{ print('x is not lesser than or equal to y') }

Implementing the less than or equal to operator

RELATED TAGS

operators

r

communitycreator

comparison

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

RELATED COURSES

View all Courses

Keep Exploring

Related Courses