R Tutorial - Logical Operators and Vectors in R

Using Relational Operators in R: A Comprehensive Guide

You are likely familiar with relational operators in R and know how to use them effectively, but what if you want to change or combine the results of these operations? In this guide, we will explore three fundamental relational operators in R - the ant operator, the inclusive OR operator, and the exclusive negation operator - and examine their functions, usage, and implications.

The Ant Operator

The ant operator is a logical operation that returns true only if both operands are true. It is denoted by `~` or `-`. The ant operator works similarly to the way you would expect it to, taking two logical values as input and returning false if either of them is false. For example, suppose we have a variable x equal to 12. We can use the expression `x > 5 & x < 15` to check if this variable is greater than 5 but less than 15. The first part of the expression will evaluate to true because 12 is indeed greater than 5. Similarly, the second part will also evaluate to true. Therefore, the result of the entire expression will be true.

However, if we were to use x equal to 17 in a similar expression `x > 5 & x < 15`, the first part would still be true because 17 is greater than 5, but the second part would be false because 17 is not less than 15. Consequently, the entire expression would evaluate to false. This makes sense, as 17 does indeed lie outside the specified range of (5, 15).

The Inclusive OR Operator

In contrast, the inclusive OR operator returns true if at least one of its operands is true. It is denoted by `|` or `||`. The inclusive OR operator works in a way that might seem counterintuitive at first: it will return true even if both operands are false.

For example, suppose we have a variable y equal to 5. We can use the expression `y < 5 | y > 15` to check if this variable is less than five or greater than 15. The first part of the expression will evaluate to false because 5 is indeed not less than five. However, the second part will be true because 5 is indeed greater than 15. Consequently, the entire expression will evaluate to true.

This behavior can lead to unexpected results if used carelessly. For instance, suppose we want to check if a value is within a certain range without including its upper limit. In such cases, using the inclusive OR operator would be incorrect and might yield false positives.

The Exclusive Negation Operator

The exclusive negation operator returns true only if one of its operands is true but not both. It can be represented as `-` or `!`. The exclusive negation operator works by first applying an AND operation (i.e., the logical `&`) between the two operands, then applying a NOT operation to the result.

For example, suppose we have a variable z equal to 5. We can use the expression `- (z > 5 & z < 15)` to check if this value is either less than five or greater than fifteen without including its upper limit. The inner part of the expression `z > 5 & z < 15` will be true because 5 is indeed greater than 5 and less than 15, thus making the AND operation evaluate to true. Then, applying the NOT operator results in false.

This behavior highlights the importance of carefully selecting which relational operators to use depending on your specific needs and requirements.

Using Relational Operators with Comparisons

Relational operators work well when used together with comparisons. For instance, we can combine a variable `x` equal to 12 with a comparison like `x > 5 & x < 15` to check if it falls within the specified range (not including its upper limit). This allows us to perform complex checks while still maintaining clarity.

Using Relational Operators with Vectors and Matrices

When dealing with vectors and matrices, relational operators also work element-wise. For example, we can apply an inclusive OR operation (`|`) between two vectors of the same length to get a new vector where each element is true if either corresponding element in the input vectors is true.

However, there's a subtle point to note when working with relational operators on vectors and matrices: the behavior can change depending on whether you use a single sign or double vertical bar (`|` vs `||`). The difference lies primarily in what exactly happens at each step. For instance, using the inclusive OR operator (`|`) always returns true if both operands are not false; whereas using its more specialized version (`||`) performs an AND operation before applying the OR.

The Importance of Awareness

It is crucial to be aware of these differences and use relational operators appropriately based on context and requirements. There's also the fact that when dealing with vectors and matrices, different versions of relational operators produce varying results at times; understanding these variations can significantly improve your skills in working with them effectively.

"WEBVTTKind: captionsLanguage: enyou 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 ant operator the ant 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 evaluates the 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 the true the second part will also evaluate the 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 r operator Works similarly but the difference is that only at least one of the logic iCal 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 R operation so in the case of false or false the result is false remember that the r operation is not an exclusive R operation so true or true equals true as well just as for ant operation s we can use comparisons together with the r operator suppose you have variable y equal to 5 this time to see if this variable is less than five 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 f y equal 14 the expression y less than 5 or y greater 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 here the not operator the not operator represented by an exclamation mark simply negates The Logical value it's used on so exclamation mark true evaluates to false while exclamation mark false evaluates to true just as the r 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 five which evaluates the true as five is a numeric if you 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 an 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 a third elements where false and false give false a similar thing happens with the r 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 truths 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 m% or ver 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 M perent we simply get true that's because a double M percent 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 sign version returns only the result of the r operator on the first element of each Vector another difference between a single and a double M perent 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 oper operations on vectors you will very likely want to use the single sign versionsyou 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 ant operator the ant 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 evaluates the 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 the true the second part will also evaluate the 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 r operator Works similarly but the difference is that only at least one of the logic iCal 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 R operation so in the case of false or false the result is false remember that the r operation is not an exclusive R operation so true or true equals true as well just as for ant operation s we can use comparisons together with the r operator suppose you have variable y equal to 5 this time to see if this variable is less than five 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 f y equal 14 the expression y less than 5 or y greater 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 here the not operator the not operator represented by an exclamation mark simply negates The Logical value it's used on so exclamation mark true evaluates to false while exclamation mark false evaluates to true just as the r 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 five which evaluates the true as five is a numeric if you 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 an 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 a third elements where false and false give false a similar thing happens with the r 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 truths 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 m% or ver 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 M perent we simply get true that's because a double M percent 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 sign version returns only the result of the r operator on the first element of each Vector another difference between a single and a double M perent 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 oper operations on vectors you will very likely want to use the single sign versions\n"