This lesson is meant to be a short introduction to logical operations in R. Show There are two logical values in R, also called boolean values. They are TRUE and FALSE. In R you can construct logical expressions which will evaluate to either TRUE or FALSE. Many of the questions in this lesson will involve evaluating logical expressions. It may be useful to open up a second R terminal where you can experiment with some of these expressions. Creating logical expressions
requires logical operators. You’re probably familiar with arithmetic operators like
Just like arithmetic, logical expressions can be grouped by parenthesis so that the entire expression (TRUE == TRUE) == TRUE evaluates to TRUE. To test out this property, try evaluating (FALSE == TRUE) == FALSE .
The equality operator can also be used to compare numbers. Use
The previous expression evaluates to FALSE because 6 is less than 7. Thankfully, there are inequality operators that allow us to test if a value is less than or greater than another value. The less than operator
There is also a less-than-or-equal-to operator
Keep in mind that there are the corresponding greater than Which of the following evaluates to FALSE?
9 >= 10 Which of the following evaluates to TRUE?
9 >= 10 The next operator we will discuss is the ‘not equals’ operator represented by
In order to negate boolean expressions you can use the NOT operator. An exclamation point
Let’s take a moment to review. The equals operator
Which of the following evaluates to FALSE?
!(0 >= -1) What do you think the following expression will evaluate to?: (TRUE != FALSE) == !(6 == 7)
TRUE At some point you may need to examine relationships between multiple logical expressions. This is where the AND operator and the OR operator come in. Let’s look at how the AND operator works. There are two AND operators in R,
You can use the
What happens in this case is that the left operand Now we’ll type the same expression except we’ll use the
In this case, the left operand is only evaluated with the first member of the right operand (the vector). The rest of the elements in the vector aren’t evaluated at all in this expression. The OR operator follows
a similar set of rules. The An expression using the OR operator will evaluate to TRUE if the left operand or the right operand is TRUE. If both are TRUE, the expression will evaluate to TRUE, however if neither are TRUE, then the expression will be FALSE. Let’s test out the vectorized version of the OR operator. Type the expression TRUE | c(TRUE, FALSE, FALSE).
Now let’s try out the non-vectorized version of the OR operator. Type the expression TRUE || c(TRUE, FALSE, FALSE).
Logical operators can be chained together just like arithmetic operators. The expressions: As you may recall, arithmetic has an order of operations and so do logical expressions. All AND operators are evaluated before OR operators. Let’s look at an example of an ambiguous case. Type: 5 > 8 || 6 != 8 && 4 > 3.9
Let’s walk through the order of operations in the above case. First the left and right operands of the AND operator are evaluated. 6 is not equal 8, 4 is greater than 3.9, therefore both operands are TRUE so the resulting expression Which one of the following expressions evaluates to TRUE?
TRUE && FALSE || 9 >= 4 && 3 < 6 Which one of the following expressions evaluates to FALSE?
FALSE && 6 >= 6 || 7 >= 8 || 50 <= 49.5 Now that you’re familiar with R’s logical operators you can take advantage of a few functions that R provides for dealing with logical expressions. The function isTRUE() takes one argument. If that argument evaluates to TRUE, the function will return TRUE. Otherwise, the function will return FALSE. Try using this function by typing: isTRUE(6 > 4)
Which of the following evaluates to TRUE?
!isTRUE(4 < 3) The function identical() will return TRUE if the two R objects passed to it as arguments are identical. Try out the identical() function by typing: identical(‘twins’, ‘twins’)
Which of the following evaluates to TRUE?
identical(5 > 4, 3 < 3.1) You should also be aware of the xor() function, which takes two arguments. The xor() function stands for exclusive OR. If one argument evaluates to TRUE and one argument evaluates to FALSE, then this function will return TRUE, otherwise it will return FALSE. Try out the xor() function by typing: xor(5 == 6, !FALSE)
5 == 6 evaluates to FALSE, !FALSE evaluates to TRUE, so xor(FALSE, TRUE) evaluates to TRUE. On the other hand if the first argument was changed to 5 == 5 and the second argument was unchanged then both arguments would have been TRUE, so xor(TRUE, TRUE) would have evaluated to FALSE. Which of the following evaluates to FALSE?
xor(4 >= 9, 8 != 8.0) For the next few questions, we’re going to need to create a vector of integers called ints. Create this vector by typing: ints <- sample(10)
Now simply display the contents of ints.
The vector
We can use the resulting logical vector to ask other questions about ints. The which() function takes a logical vector as an argument and returns the indices of the vector that are TRUE. For example which(c(TRUE, FALSE, TRUE)) would return the vector c(1, 3). Use the which() function to find the indices of ints that are greater than 7.
Which of the following commands would produce the indices of the elements in ints that are less than or equal to 2?
which(ints <= 2) Like the which() function, the functions any() and all() take logical vectors as their argument. The any() function will return TRUE if one or more of the elements in the logical vector is TRUE. The all() function will return TRUE if every element in the logical vector is TRUE. Use the any() function to see if any of the elements of ints are less than zero.
Use the all() function to see if all of the elements of ints are greater than zero.
Which of the following evaluates to TRUE?
any(ints == 10) That’s all for this introduction to logic in R. If you really want to see what you can do with logic, check out the control flow lesson! Please submit the log of this lesson to Google Forms so that Simon (well, ok, Andrew) may evaluate your progress.
Yessiree, bob! What type of operator determines whether a specific relationship exists between two values?Relational Operators and Boolean Expressions
A Relational Operator determines whether a specific relationship exists between two values.
Which one of the following is the not equal operator?The equal-to operator ( == ) returns true if both operands have the same value; otherwise, it returns false . The not-equal-to operator ( != ) returns true if the operands don't have the same value; otherwise, it returns false .
When a relational expression is false it has the value?A relational expression evaluates to 1 if the relation is true, and evaluates to 0 if the relation is false.
Is a boolean variable that signals when some condition exists in the program? A Flag is a boolean variable that signals when some condition exists in a program. When a flag is set to true, it means some condition exists When a flag is set to false, it means some condition does not exist.
|