2.4 More on Logical Vectors

Consider the following expression:

## [1] TRUE

We constructed it with the “less-than” operator <. You can think of it as saying that 13 is less than 20, which is a true statement, and sure enough, R evaluates the expression 13 < 20 as TRUE.

When you think about it, we’ve seen lots of expressions so far. Here are just a few of them:

  • sqrt(64)
  • heights
  • heights[1:3]
  • 13 < 20

When we type any one of them into the console, it evaluates to a particular value. In the examples above, the value was always a vector.

Expressions like 13 < 20 that evaluate to a logical vector are often called Boolean expressions.2

2.4.1 Boolean Operators

Let’s look further into Boolean expressions. Define the following two vectors:

Now let’s evaluate the expression a < b:


The < operator, when applied to vectors, always works element-wise; that is, it is applied to corresponding elements of the vectors on either side of it. R’s evaluation of a < b involves evaluation of the following three expressions:

  • 10 < 8 (evaluates to FALSE)
  • 13 < 15(evaluates to TRUE)
  • 17 < 12(evaluates to FALSE)

The result is a logical vector of length 3.

The < operator is an example of a Boolean operator in R. Table 2.4.1 shows the available Boolean operators.

Table 2.1: The Boolean Operators
Operation What It Means
< less than
> greater than
<= less than or equal to
>= greater than or equal to
== equal to
& and
| or
&& and (scalar version)
|| or (scalar version)
! not Inequalities

The “numerical-looking operators” (<, <=, >, >=) have their usual meanings when one is working with numerical vectors3 When applied to character vectors they evaluate according to an alphabetical order:


The reasons for the evaluation above are as follows:

  • D comes before t in the alphabet;
  • lowercase t comes before uppercase T, according to R;
  • characters for numbers come before letter-characters, according to R. Equality

The equality (==) operator indicates whether the expressions being compared evaluate to the same value. Note that it’s made with two equal-signs, not one! It’s all about evaluation to the same value, not strict identity. The following examples will help to clarify this.

## Dorothy    Toto 
##    TRUE    TRUE

(Note that the resulting logical vector inherits the names of a, the vector on the left.).

But a and b aren’t identical. We can see this because R has the function identical() to test for identity:

## [1] FALSE

Corresponding elements of a and b have the same values, but the two vectors don’t have the same set of names, so they aren’t considered identical.

Here’s another way to see that “evaluating to the same value” is not the same as “identity”:

## [1] TRUE

When TRUE (itself oftype logical) is being compared with something numerical (type integer or double) it is coerced into the numerical vector 1. (In the same situation FALSE would be coerced to 0.) But clearly TRUE and 1 are not identical:

## [1] FALSE And, Or, Not

We consider an “and” statement to be true when both of its component statements are true; otherwise it is counted as false. The & Boolean operator accords with our thinking:


In logic and mathematics, an “or” statement is considered to be true when at least one of its component statements are true. (This is sometimes called the “inclusive” use of the term “or.”) R accords with this line of thinking:


The && and || operators follow the “and” and “or” logic respectively, but are applied only to the first elements of the vectors being compared:

## [1] TRUE
## [1] TRUE

These operators will come in handy later on, when we study conditionals.

The final Boolean operator is !, which works like “not”:

## [1] FALSE  TRUE

  1. So-called after George Boole, a nineteenth century British logician.

  2. A vector is said to be numerical if it is of type integer or double.