2.5 Vector Recycling
Consider the vector
<- c(2, 6, 1, 7, 3) vec
Look at what happens when we evaluate the expression:
> 4 vec
## [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:
<- 1:6
vec2 > c(3,1) vec2
## [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:
<- 1:7
vec2 > c(3, 8) vec2
## 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:
<- c("Dorothy", "Scarecrow", "Tin Man", "Lion", "Toto")
person <- c(12, 0.04, 15, 18, 6)
age <- c(TRUE, FALSE, TRUE, FALSE, TRUE) likesDogs
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.
Write a Boolean expression that is
TRUE
when a person is less than 14 years old andFALSE
otherwise.Write a Boolean expression that is
TRUE
when a person is between 10 and 15 years old (not including 10 but not 15) andFALSE
otherwise.Write a Boolean expression that is
TRUE
when a person is more than 12 years old and likes dogs, andFALSE
otherwise.Write a Boolean expression that is
TRUE
when a person is more than 12 years old and does not like dogs, andFALSE
otherwise.Write a Boolean expression that is
TRUE
when a person is more than 12 years old and or likes dogs, andFALSE
otherwise.Write a Boolean expression that is
TRUE
when the person is Dorothy, andFALSE
otherwise.Write a Boolean expression that is
TRUE
when the person is Dorothy or Tin Man, andFALSE
otherwise.Write a Boolean expression that is
TRUE
when the person’s name comes after the letter “M” in the alphabet, andFALSE
otherwise.Write a Boolean expression that is
FALSE
when the person is Dorothy, andTRUE
otherwise.
2.5.2 Solutions to Practice Exercises
Here’s the code:
< 14 age
## [1] TRUE TRUE FALSE FALSE TRUE
Here’s the code:
>= 10 & age < 15 age
## [1] TRUE FALSE FALSE FALSE FALSE
Here’s the code:
> 12 & likesDogs age
## [1] FALSE FALSE TRUE FALSE FALSE
Here’s the code:
> 12 & !likesDogs age
## [1] FALSE FALSE FALSE TRUE FALSE
Here’s the code:
> 12 | likesDogs age
## [1] TRUE FALSE TRUE TRUE TRUE
Here’s the code:
== "Dorothy" person
## [1] TRUE FALSE FALSE FALSE FALSE
Here’s the code:
== "Dorothy" | person == "Tin Man" person
## [1] TRUE FALSE TRUE FALSE FALSE
Here’s the code:
> "M" person
## [1] FALSE TRUE TRUE FALSE TRUE
Here’s the code:
!= "Dorothy" person
## [1] FALSE TRUE TRUE TRUE TRUE