Bird
Raised Fist0
R Programmingprogramming~20 mins

Vector recycling behavior in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Vector Recycling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of vector recycling in addition
What is the output of this R code?
c(1, 2, 3, 4) + c(10, 20)
R Programming
c(1, 2, 3, 4) + c(10, 20)
A[1] 11 22 13 24
B[1] 11 22 33 44
C[1] 11 22 31 42
D[1] 11 12 13 14
Attempts:
2 left
💡 Hint
Remember that R recycles the shorter vector to match the length of the longer one.
❓ Predict Output
intermediate
2:00remaining
Output of vector recycling with logical vectors
What is the output of this R code?
c(TRUE, FALSE, TRUE) & c(FALSE, TRUE)
R Programming
c(TRUE, FALSE, TRUE) & c(FALSE, TRUE)
A[1] FALSE FALSE FALSE
B[1] FALSE FALSE TRUE
C[1] FALSE TRUE FALSE
D[1] TRUE TRUE TRUE
Attempts:
2 left
💡 Hint
The shorter vector is recycled to match the longer vector length before element-wise AND.
🔧 Debug
advanced
2:00remaining
Error caused by vector recycling with incompatible lengths
What error does this R code produce?
c(1, 2, 3) + c(10, 20, 30, 40)
R Programming
c(1, 2, 3) + c(10, 20, 30, 40)
AError: object lengths differ and cannot be recycled
BError: non-numeric argument to binary operator
CNo error, output is [1] 11 22 33 44
DWarning: longer object length is not a multiple of shorter object length
Attempts:
2 left
💡 Hint
R recycles shorter vectors but warns if lengths are not multiples.
🧠 Conceptual
advanced
2:00remaining
Understanding recycling with named vectors
Given these named vectors in R:
a <- c(x=1, y=2, z=3)
b <- c(y=10, z=20)

What is the output of a + b?
R Programming
a <- c(x=1, y=2, z=3)
b <- c(y=10, z=20)
a + b
AError: non-numeric argument to binary operator
BWarning about recycling and output: 1 NA NA
CNumeric vector: 11 22 23
DWarning about recycling and output: NA 12 23
Attempts:
2 left
💡 Hint
Named vectors do not recycle by matching names in arithmetic operations.
❓ Predict Output
expert
2:00remaining
Output length after recycling with complex vectors
What is the length of the vector produced by this R code?
length(c(1, 2, 3, 4, 5) + c(10, 20))
R Programming
length(c(1, 2, 3, 4, 5) + c(10, 20))
A2
BError: lengths differ and cannot be recycled
C5
D10
Attempts:
2 left
💡 Hint
The result length matches the longer vector length after recycling.

Practice

(1/5)
1. What happens in R when you add two vectors of different lengths, like c(1, 2, 3) + c(4, 5)?
easy
A. R throws an error because vector lengths differ.
B. Only the first two elements are added, and the rest are ignored.
C. The shorter vector is repeated to match the longer vector's length before addition.
D. The result is a vector of length 2 with NA values.

Solution

  1. Step 1: Understand vector lengths

    The first vector has length 3, the second has length 2.
  2. Step 2: Apply recycling rule

    The shorter vector c(4, 5) is repeated to become c(4, 5, 4) to match length 3.
  3. Final Answer:

    The shorter vector is repeated to match the longer vector's length before addition. -> Option C
  4. Quick Check:

    Vector recycling repeats shorter vector [OK]
Hint: Short vector repeats to match longer vector length [OK]
Common Mistakes:
  • Thinking R throws an error for different lengths
  • Assuming vectors must be same length without recycling
  • Ignoring recycling and expecting NA values
2. Which of the following is the correct syntax to add two vectors a and b in R, using vector recycling?
easy
A. a + b
B. add(a, b)
C. a & b
D. sum(a, b)

Solution

  1. Step 1: Identify vector addition syntax in R

    In R, adding vectors element-wise is done with the + operator.
  2. Step 2: Check other options

    add() is not a base R function; & is logical AND; sum() sums all elements, not element-wise.
  3. Final Answer:

    a + b -> Option A
  4. Quick Check:

    Use + for element-wise vector addition [OK]
Hint: Use + operator for vector addition in R [OK]
Common Mistakes:
  • Using sum() which sums all elements, not element-wise
  • Using & which is logical AND, not addition
  • Assuming add() is a base R function
3. What is the output of the following R code?
v1 <- c(10, 20, 30, 40)
v2 <- c(1, 2)
v1 + v2
medium
A. [11, 22, 33, 44]
B. [11, 22, 31, 42]
C. [11, 22, 31, 41]
D. Error due to length mismatch

Solution

  1. Step 1: Check vector lengths

    v1 has length 4, v2 has length 2.
  2. Step 2: Apply recycling

    v2 repeats as c(1, 2, 1, 2). Adding element-wise:
    10+1=11, 20+2=22, 30+1=31, 40+2=42.
  3. Final Answer:

    [11, 22, 31, 42] -> Option B
  4. Quick Check:

    Recycle shorter vector and add element-wise [OK]
Hint: Repeat shorter vector to match length, then add [OK]
Common Mistakes:
  • Adding only first two elements and ignoring recycling
  • Expecting error due to different lengths
  • Misaligning recycled elements
4. Consider this R code:
x <- c(5, 10, 15)
y <- c(2, 4, 6, 8)
z <- x + y

What is the problem and how to fix it?
medium
A. Lengths differ and 3 does not divide 4; fix by making lengths compatible.
B. No problem; code runs fine with recycling.
C. Syntax error due to missing parentheses; add parentheses.
D. Vectors must be same length; truncate longer vector.

Solution

  1. Step 1: Check vector lengths

    x length is 3, y length is 4.
  2. Step 2: Understand recycling warning

    3 does not divide 4 evenly, so R will recycle x but give a warning about length mismatch.
  3. Step 3: Fix the problem

    Make vector lengths compatible, e.g., use length 6 or 12, or truncate longer vector.
  4. Final Answer:

    Lengths differ and 3 does not divide 4; fix by making lengths compatible. -> Option A
  5. Quick Check:

    Unequal lengths without exact multiple cause warnings [OK]
Hint: Lengths must be multiples to avoid warnings [OK]
Common Mistakes:
  • Ignoring recycling warning
  • Assuming code runs without warnings
  • Trying to fix with syntax changes
5. You have two vectors:
v1 <- c(3, 6, 9, 12, 15)
v2 <- c(1, 2)

You want to multiply them element-wise without warnings and get a vector of length 7 by repeating both vectors appropriately. Which code achieves this?
hard
A. rep(v1, each=2) * rep(v2, times=5)
B. v1 * v2
C. v1 + v2
D. rep(v1, length.out=7) * rep(v2, length.out=7)

Solution

  1. Step 1: Understand desired output length

    You want a length 7 vector, so both vectors must be length 7.
  2. Step 2: Use rep() with length.out

    Using rep(v1, length.out=7) repeats v1 to length 7; similarly for v2.
  3. Step 3: Multiply element-wise

    Multiplying these repeated vectors gives length 7 without warnings.
  4. Final Answer:

    rep(v1, length.out=7) * rep(v2, length.out=7) -> Option D
  5. Quick Check:

    Repeat vectors to same length before operation [OK]
Hint: Use rep() with length.out to match lengths [OK]
Common Mistakes:
  • Using direct multiplication causing warnings
  • Using rep() with each or times incorrectly
  • Adding vectors instead of multiplying
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard