Knowledge Quiz: Vectors

Which of the following expressions generates the integer vector 1, 2, 3, 4, 5?

  • Wrong. This command will return a length-one vector containing only 1.
  • Wrong. This command will return a length-25 vector that repeats the integers from 1 to 5 five times.
  • Correct.
  • Wrong. This command will return a length-five vector containing the integers from -1 to 3.
  • Correct.

What is the value of a * b, given the following vectors?

\[ \begin{align*} a &= 3, 5, 1\\ b &= 5, 2, 3 \end{align*} \]

Note: Incorrectly formatted answers won’t be recognized as correct.

  • Format you answer as element1, element2, element3, ...
  • Separate each element with a comma and a space.
  • Write all numbers without decimal places (e.g., write 8, not 8.0).

The a * b implements element-wise multiplication, so the result is 15, 10, 3.

a <- 1:6 and b <- 1:2 are two vectors. When you run a + b, R recycles the elements of b without printing a warning. Why doesn’t R warn you in this case?

R only warns about recycling when the shorter vector’s length does not evenly divide the longer vector’s length. In those cases, at least one “leftover” element gets used a different number of times than the rest, which is often a sign of a mistake. When the shorter length divides the longer one evenly (as with lengths 6 and 2 here), every element of the shorter vector is reused the same number of times, so R treats the recycling as intentional and stays silent.

Note
  • Click the check-mark button to check your answer.
  • Click the question-mark button to see an explanation of the solution.
Back to top