Relational Operators in R
Free Preview
You're previewing a lesson from R Programming for Data Science
Here's everything waiting for you in the full course:
Test Your Knowledge
Check your understanding of this lesson with a short quiz.
Free Preview
Here's everything waiting for you in the full course:
Check your understanding of this lesson with a short quiz.
Ask questions about this lesson and get instant answers.
Relational Operators are used to compare values in R objects. There are six relational operators:
1| Operator | Description |
2| --- | --- |
3| `<` | Less than |
4| `>` | Greater than |
5| `<=` | Less than or equal to |
6| `>=` | Greater than or equal to |
7| `==` | Equal to |
8| `!=` | Not equal to |
9Each 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.
>)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.
1The following are two vectors containing returns from two stocks over the past five days.
2stock_A <- c(10, 8, 9, 11, 12)
3stock_B <The result will be as follows:
[1] 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.
<)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.
1> stock_A <- c(10, 8, 9, 11, 12)
2> stock_B <- c(8,==)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:
1> stock_A <- c(10, 8, 9, 11, 12)
2> stock_B <- c(8,!=)The Not Equal (!=) operator checks if each element of the first vector is unequal to the corresponding element of the second vector.
1> # The following are two vectors containing returns from two stocks over the past five days.
2>
3> stock_A <- c(10, 8, 9, 11, >=)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.
1> # The following are two vectors containing returns from two stocks over the past five days.
2>
3> stock_A <- c(10, 8, 9, 11, Compare this with the Greater Than (>) operator. Everything else is same, just the last element now returns TRUE (because of the equal to condition).
<=)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.
1> stock_A <- c(10, 8, 9, 11, 12)
2> stock_B <- c(8,