Want to learn more? Take the full course at https://learn.datacamp.com/courses/in... at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.
----
The and operator works just as you would expect. It typically takes two logical values and returns `TRUE` only if both the logical values are `TRUE` themselves. This means that `TRUE and TRUE` evaluates `TRUE`, but that `FALSE and TRUE`, `TRUE and FALSE` and `FALSE and FALSE` all evaluate to `FALSE`.
Instead of using logical values, we can off course 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. 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 & 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 OR operation to evaluate to TRUE. This means that, `TRUE or TRUE` equals `TRUE`, but that also `TRUE | FALSE` and `FALSE | 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, `TRUE or TRUE` equals `TRUE` as well.
Just as for AND operators, we can use comparisons together with the OR operator. Suppose we have a 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` equal 14. The expression y less than 5 or y greater than 15 now evaluates to FALSE | 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 marks 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 a numeric. If we negate this result using the NOT operator,
!is.numeric(5) we get false. If, however, we type is.numeric("hello") we get `FALSE`. Negating this results in `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 | TRUE` gives `TRUE`, `TRUE | FALSE` also gives `TRUE`, and `FALSE | FALSE` gives `FALSE`. The NOT operator also works on every element of the vector: `TRUE`s are converted to `FALSE`s, and `FALSE`s are converted to `TRUE`s.
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 the result of the logical operation you're carrying out can be different. The biggest difference occurs when you use the two types of operators 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 OR operator. The single sign version returns an entire vector. The double sign version returns only the result of the OR operator on the first element of each vector.
Another difference between a single and a double ampersand that is less obvious has something to do with control structures, but that's more advanced material.
#DataCamp #RTutorial #IntermediateR
Информация по комментариям в разработке