2.5 Vector Recycling

Consider the vector

vec <- c(2, 6, 1, 7, 3)

Look at what happens when we evaluate the expression:

vec > 4
## [1] FALSE  TRUE FALSE  TRUE FALSE

At first blush this doesn’t make any sense: vec has length 5, whereas 4 is a vector of length 1. How can the two of them be compared?

They cannot, in fact, be compared. Instead the shorter of the two vectors—the 4—is recycled into the c(4,4,4,4,4) a vector of length five, which may then be compared element-wise with vec. Recycling is a great convenience as it allows us to express an idea clearly and concisely.

Recycling is always performed on the shorter of two vectors. Consider the example below:

vec2 <- 1:6
vec2 > c(3,1)
## [1] FALSE  TRUE FALSE  TRUE  TRUE  TRUE

Here, c(3,1) was recycled into c(3,1,3,1,3,1) prior to being compared with vec2.

What happens if the length of the longer vector is not a multiple of the shorter one? We should look into this:

vec2 <- 1:7
vec2 > c(3, 8)
## longer object length is not a multiple of shorter object length
## [1] FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE

We get a warning, but R tries to do the job for us anyway, recycling the shorter vector to c(3,8,3,8,3,8,3) and then performing the comparison.

By the way, if you don’t want to see the warning you can put the expression into the suppressWarnings() function:

suppressWarnings(vec2 > c(3, 8))
## [1] FALSE FALSE FALSE FALSE  TRUE FALSE

2.5.1 Practice Exercises

We’ll work with the following vectors:

person <- c("Dorothy", "Scarecrow", "Tin Man", "Lion", "Toto")
age <- c(12, 0.04, 15, 18, 6)
likesDogs <- c(TRUE, FALSE, TRUE, FALSE, TRUE)

Think of the vectors as having corresponding elements. Thus, there is a person named Dorothy who is 12 years old and likes dogs, a person named Tin Man who is 0.04 years old and doesn’t like dogs, etc.

  1. Write a Boolean expression that is TRUE when a person is less than 14 years old and FALSE otherwise.

  2. Write a Boolean expression that is TRUE when a person is between 10 and 15 years old (not including 10 but not 15) and FALSE otherwise.

  3. Write a Boolean expression that is TRUE when a person is more than 12 years old and likes dogs, and FALSE otherwise.

  4. Write a Boolean expression that is TRUE when a person is more than 12 years old and does not like dogs, and FALSE otherwise.

  5. Write a Boolean expression that is TRUE when a person is more than 12 years old and or likes dogs, and FALSE otherwise.

  6. Write a Boolean expression that is TRUE when the person is Dorothy, and FALSE otherwise.

  7. Write a Boolean expression that is TRUE when the person is Dorothy or Tin Man, and FALSE otherwise.

  8. Write a Boolean expression that is TRUE when the person’s name comes after the letter “M” in the alphabet, and FALSE otherwise.

  9. Write a Boolean expression that is FALSE when the person is Dorothy, and TRUE otherwise.

2.5.2 Solutions to Practice Exercises

  1. Here’s the code:

    age < 14
    ## [1]  TRUE  TRUE FALSE FALSE  TRUE
  2. Here’s the code:

    age >= 10 & age < 15
    ## [1]  TRUE FALSE FALSE FALSE FALSE
  3. Here’s the code:

    age > 12 & likesDogs
    ## [1] FALSE FALSE  TRUE FALSE FALSE
  4. Here’s the code:

    age > 12 & !likesDogs
    ## [1] FALSE FALSE FALSE  TRUE FALSE
  5. Here’s the code:

    age > 12 | likesDogs
    ## [1]  TRUE FALSE  TRUE  TRUE  TRUE
  6. Here’s the code:

    person == "Dorothy"
    ## [1]  TRUE FALSE FALSE FALSE FALSE
  7. Here’s the code:

    person == "Dorothy" | person == "Tin Man"
    ## [1]  TRUE FALSE  TRUE FALSE FALSE
  8. Here’s the code:

    person > "M"
    ## [1] FALSE  TRUE  TRUE FALSE  TRUE
  9. Here’s the code:

    person != "Dorothy"
    ## [1] FALSE  TRUE  TRUE  TRUE  TRUE