• No results found

Multiple Comparisons: Logical Operators

In document The Book of R (Page 98-101)

3.4 Multidimensional Arrays

4.1.3 Multiple Comparisons: Logical Operators

Logicals are especially useful when you want to examine whether multiple conditions are satisfied. Often you’ll want to perform certain operations only if a number of different conditions have been met.

The previous section looked at relational operators, used to com- pare the literal values (that is, numeric or otherwise) of stored R objects. Now you’ll look at logical operators, which are used to compare twoTRUE

orFALSEobjects. These operators are based on the statements AND and OR. Table 4-2 summarizes the R syntax and the behavior of logical opera- tors. The AND and OR operators each have a “single” and “element-wise” version—you’ll see how they’re different in a moment.

Table 4-2: Logical Operators Comparing Two Logical Values Operator Interpretation Results

&

TRUE & TRUEisTRUE AND TRUE & FALSEisFALSE (element-wise) FALSE & TRUEisFALSE FALSE & FALSEisFALSE && AND Same as&above

(single comparison)

|

TRUE|TRUEisTRUE OR TRUE|FALSEisTRUE (element-wise) FALSE|TRUEisTRUE FALSE|FALSEisFALSE

|| OR Same as|above

(single comparison)

! NOT !TRUEisFALSE !FALSEisTRUE

The result of using any logical operator is a logical value. An AND com- parison is true only if both logicals areTRUE. An OR comparison is true if at least one of the logicals isTRUE. The NOT operator (!) simply returns the opposite of the logical value it’s used on. You can combine these operators to examine multiple conditions at once.

R> FALSE||((T&&TRUE)||FALSE) [1] TRUE R> !TRUE&&TRUE [1] FALSE R> (T&&(TRUE||F))&&FALSE [1] FALSE R> (6<4)||(3!=1) [1] TRUE

As with numeric arithmetic, there is an order of importance for logical operations in R. An AND statement has a higher precedence than an OR statement. It’s helpful to place each comparative pair in parentheses to pre- serve the correct order of evaluation and make the code more readable. You can see this in the first line of this code, where the innermost comparison is the first to be carried out:T&&TRUEresults inTRUE; this is then provided as one of the logical values for the next bracketed comparison whereTRUE||FALSE

results inTRUE. The final comparison is thenFALSE||TRUE, and the result,TRUE, is printed to the console. The second line reads as NOTTRUEANDTRUE, which of course returnsFALSE. In the third line, once again the innermost pair is evaluated first:TRUE||FisTRUE;T&&TRUEisTRUE; and finallyTRUE&&FALSE

isFALSE. The fourth and final example evaluates two distinct conditions in parentheses, which are then compared using a logical operator. Since6<4is

FALSEand3!=1isTRUE, that gives you a logical comparison ofFALSE||TRUEand a final result ofTRUE.

In Table 4-2, there is a short (&,|) and long (&&,||) version of the AND and OR operators. The short versions are meant for element-wise compar- isons, where you have two logical vectors and you want multiple logicals as a result. The long versions, which you’ve been using so far, are meant for comparing two individual values and will return a single logical value. This is important when programming conditional checks in R in anifstatement, which you’ll look at in Chapter 10. It’s possible to compare a single pair of logicals using the short version—though it’s considered better practice to use the longer versions when a singleTRUE/FALSEresult is needed.

Let’s look at some examples of element-wise comparisons. Suppose you have two vectors of equal length,fooandbar:

R> foo <- c(T,F,F,F,T,F,T,T,T,F,T,F) R> foo

[1] TRUE FALSE FALSE FALSE TRUE FALSE TRUE TRUE TRUE FALSE TRUE FALSE

and

R> bar <- c(F,T,F,T,F,F,F,F,T,T,T,T) R> bar

[1] FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE

The short versions of the logical operators match each pair of elements by position and return the result of the comparison.

R> foo&bar

[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE FALSE R> foo|bar

[1] TRUE TRUE FALSE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE TRUE

Using the long version of the operators, on the other hand, means R carries out the comparison only on the first pair of logicals in the two vectors.

R> foo&&bar [1] FALSE R> foo||bar [1] TRUE

Notice that the last two results match the first entries of the vectors you got using the short versions of the logical operators.

66 Chapter 4

Exercise 4.2

a. Store the vectorc(7,1,7,10,5,9,10,3,10,8)asfoo. Identify the elements greater than 5 OR equal to 2.

b. Store the vectorc(8,8,4,4,5,1,5,6,6,8)asbar. Identify the ele- ments less than or equal to 6 AND not equal to 4.

c. Identify the elements that satisfy (a) infooAND satisfy (b) inbar. d. Store a third vector calledbazthat is equal to the element-wise

sum offooandbar. Determine the following:

i. The elements ofbazgreater than or equal to 14 but not equal to 15

ii. The elements of the vector obtained via an element-wise division ofbazbyfoothat are greater than 4 OR less than or equal to 2

e. Confirm that using the long version in all of the preceding exercises performs only the first comparison (that is, the results each match the first entries of the previously obtained vectors).

In document The Book of R (Page 98-101)