R Tutorial - Logical Operators and Vectors in R

Learn R's logical operators: & and | evaluate every element in a vector and return a full logical vector, while && and || check only the first element. The NOT operator (!) inverts logical results, useful for negating functions like is.numeric().
Chapters
You know how to use relational operators in R? Awesome. But what if you want to change or combine results of these comparisons? R does this using the and, the or and the not operator. Let's have a closer look at each one of them and start with the and operator. The and operator works just as you would expect. It typically takes two logical values and returns true only if both these logical values are true themselves. This means that true and true evaluate to true, but that false and true, true and false and false and false all evaluate to false. Instead of using logical values, we can of course use the results of comparisons. Suppose you 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. As you already learned, the first part will evaluate to true. The second part will also evaluate to true. So the result of this expression is true. This makes sense because 12 lies between 5 and 15. However, if x were equal to 17, the expression x greater than 5 and x less than 15 would simplify to true and false, which results in this expression being false. The OR operator works similarly, but the difference is that only at least one of the logical values it uses should be equal to true for the entire R operation to evaluate to true. This means that true or true equals true, but that also true or false and false or true evaluate to true when both logicals are false in an OR operation. So in the case of false or false, the result is false. Remember that the OR operation is not an exclusive OR operation, so true or true equals true as well. Just as for and operators, we can use comparisons together with the OR operator. Suppose you have variable y equal to 5 this time to see if this variable is less than 5 or greater than 15. We can use this expression R will first carry out the comparisons resulting in true or false, which in turn results in true. Now let's have y 14. The expression y +5 or y than 15 now evaluates to false or false. Neither one of the comparisons are true, so the result is false. There's one last operator I want to talk about, the not operator. The not operator, represented by an exclamation mark, simply negates the logical value it's used on. So true evaluates to false, while false evaluates to true. Just as the or and and operators, you can use the not operator in combination with logical operators. This is not always necessary, however, because this line of code is exactly the same as this one. However, there are cases in R where the not Operator is really handy. For example, the built in R function is numeric checks if an R object is a numeric. As an Illustration take is numeric 5 which evaluates to true as 5 is numeric. If we negate this result using the not operator, we get false. If however we type is numeric, hello, we get false. Negating this result gives us true. Now, how do logical operators work with vectors and matrices? Well, just as relational operators, they perform the operations element wise. The and operation on these two vectors results in a vector with the elements true, false and false. The first elements in both vectors are true, so the first element of the resulting vector contains true. Similarly for the second elements where true and false result in false and the third elements where false and false give false. A similar thing happens with the or operator. True or true gives true. True or false also gives true and false or false gives false. The not operator also works on every element of the vector. Trues are converted to falses and falses are converted to trues. Now, there's one last thing I want to warn you about. It's about the difference between a single and a double ampersand or vertical bar in R. You can use both the single sign version or the double sign version. But result of the logical operation you're carrying out can be different. The biggest difference occurs when you use the two types of operations on vectors. As we've seen before, this expression evaluates to a vector containing true, false and false. However, if we use a double ampersand, we simply get true. That's because the double ampersand operation only examines the first element of each vector. In this case, the first elements are true and true. So the expression returns true. You can see similar things happening with the R operator. The single sign version returns an entire vector, while the double signed version returns only the result of the R operator on the first element of each vector. Another difference between a single and a double ampersand or vertical bar that is less obvious has something to do with control structures. But that's more advanced material. For now, just remember that you have to pay attention when doing logical operations on vectors. You will very likely want to use the single sign versions.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







