# Relational Operators in R

Relational Operators are used to compare values in R objects. There are six relational operators:

OperatorDescription
<Less than
>Greater than
<=Less than or equal to
>=Greater than or equal to
\==Equal to
!=Not equal to

Each element of the first vector is compared with the corresponding element of the second vector. The result of comparison is a Boolean value.

Let's look at each of these operators in detail.

### Greater Than (>)

The greater than (>) symbol checks if each element of the first vector is greater than the corresponding element of the second vector. The result will be a vector with logical values (TRUE or FALSE) depending on whether the condition is true or not.

The following are two vectors containing returns from two stocks over the past five days.
stock_A <- c(10, 8, 9, 11, 12)
stock_B <- c(8, 11, 10, 10, 12)
# Compare if Stock A's returns are higher than Stock B's returns
stock_A > stock_B


The result will be as follows:

  TRUE FALSE FALSE  TRUE FALSE


As you can see, Stock A's returns were higher than Stock B on day 1 (10 > 8) and day 4 (11 > 10). So, for these days, the resulting vector contains TRUE, while for the rest of the days, it is FALSE. Specially observe the last day where the returns were same for both the stocks (12 and 12). Since the condition was exclusively checking for returns being greater, the condition is FALSE here.

### Less Than (<)

The less than (<) symbol checks if each element of the first vector is less than the corresponding element of the second vector. This is just the opposite of the 'greater than' operator.

> stock_A <- c(10, 8, 9, 11, 12)
> stock_B <- c(8, 11, 10, 10, 12)
>
> #compare if Stock A's returns are less than Stock B's returns
>
> stock_A < stock_B
 FALSE  TRUE  TRUE FALSE FALSE


### Equal (==)

The equal (==) operator checks if each element of the first vector is equal to the corresponding element of the second vector. In our vectors, this condition meets only for the returns on the last day (12 == 12) as shown below:

> stock_A <- c(10, 8, 9, 11, 12)
> stock_B <- c(8, 11, 10, 10, 12)
>
> #compare if Stock A's returns are equal to Stock B's returns
>
> stock_A == stock_B
 FALSE FALSE FALSE FALSE  TRUE


### Not Equal To (!=)

The Not Equal (!=) operator checks if each element of the first vector is unequal to the corresponding element of the second vector.

> # The following are two vectors containing returns from two stocks over the past five days.
>
> stock_A <- c(10, 8, 9, 11, 12)
> stock_B <- c(8,11,10,10,12)
>
> #compare if Stock A's returns are not equal to Stock B's returns
>
> stock_A != stock_B
  TRUE  TRUE  TRUE  TRUE FALSE


### Greater Than Equal To (>=)

The Greater Than Equal To (>=) operator checks if each element of the first vector is greater than or equal to the corresponding element of the second vector.

> # The following are two vectors containing returns from two stocks over the past five days.
>
> stock_A <- c(10, 8, 9, 11, 12)
> stock_B <- c(8,11,10,10,12)
>
> #compare if Stock A's returns are greater than or equal to Stock B's returns
>
> stock_A >= stock_B
  TRUE FALSE FALSE  TRUE  TRUE


Compare this with the Greater Than (>) operator. Everything else is same, just the last element now returns TRUE (because of the equal to condition).

### Less Than Equal To (<=)

The Less Than Equal To (<=) operator checks if each element of the first vector is less than or equal to the corresponding element of the second vector.

> stock_A <- c(10, 8, 9, 11, 12)
> stock_B <- c(8,11,10,10,12)
>
> #compare if Stock A's returns are lower than or equal to Stock B's returns
>
> stock_A <= stock_B
 FALSE  TRUE  TRUE FALSE  TRUE  