Suppose we want to change or compare the results of the comparisons made using relational operators. How would we go about doing that?

R does this using the AND, the OR, and the **NOT **operator.

Logical Operators

  • AND operator &
  • OR operator |
  • NOT operator !

AND Operator “&”

The AND operator takes two logical values and returns TRUE only if both values are TRUE themselves. This means that TRUE & TRUE evaluates to TRUE, but that FALSE & TRUETRUE & FALSE, and FALSE & FALSE evaluates to FALSE.

Only two trues can give us a true with the AND operator.

Only TRUE and TRUE will give us TRUE.

Instead of using logical values, we can use the results of comparisons. Suppose we have a variable x, equal to 12. To check if this variable is greater than 5 but less than 15, we can use x greater than 5 and x less than 15.

x <- 12
x > 5 & x < 15

The first part, x > 5 will evaluate to TRUE since 12 is greater than 5. The second part, x < 15 will also evaluate to TRUE since 12 is also less than 15. So, the result of this expression is TRUE since TRUE & TRUE is TRUE. This makes sense, because 12 lies between 5 and 15.

However, if x were 17, the expression x > 5 & x < 15 would simplify to TRUE & FALSE, which results in the expression being FALSE.

For you to try

Consider the following vector and variable:

linkedin <- c(16, 9, 13, 5, 2, 17, 14)
last <- tail(linkedin, 1)

The linkedin vector represents the number of LinekdIn views you profile has gotten in the last seven days. The last variable represents the last value of the linkedin vector.

Determine whether the last variable is between 15 and 20, excluding 15 but including 20.

Solution

# We are looking for the R equivalent of 15 < last <= 20
last > 15 & last <= 20

Image for post

Image for post

The last variable of linkedin is 14, which is not between 15 and 20.

For you to try (2)

Consider the following vectors:

linkedin <- c(16, 9, 13, 5, 2, 17, 14)
facebook <- c(17, 7, 5, 16, 8, 13, 14)

The linkedin vector represents the views on your LinkedIn profile from the past 7 days, and the facebook vector represents the views on your Facebook profile from the past 7 days.

Determine when LinkedIn views exceeded 10 and Facebook views failed to reach 10 for a particular day. Use the linkedin and facebook vectors.

Solution

# linkedin exceeds 10 but facebook below 10
linkedin > 10 & facebook < 10

Image for post

Image for post

Only on the third day were the LinkedIn views greater than 10 but the Facebook views less than 10.

For you to try (3)

Consider the following matrix:

views <- matrix(c(linkedin, facebook), nrow = 2, byrow = TRUE)

The linkedin and facebook variable corresponds to the same vectors in the previous for you to try.

The matrix views has the first and second row corresponding to the linkedin and facebook vectors, respectively.

Determine when the views matrix equals to a number between 11 and 14, excluding 11 and including 14.

Solution

# When is views between 11 (exclusive) and 14 (inclusive)?
views > 11 & views <= 14

#data-analytics #data-analysis #r #r-programming #data-science

The Complete Guide to Logical Operators in R
1.25 GEEK