Bird
Raised Fist0
R Programmingprogramming~10 mins

Vector recycling behavior in R Programming - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a vector that repeats the elements of c(1, 2) to length 6.

R Programming
rep(c(1, 2), [1] = 3)
Drag options to blanks, or click blank then click option'
Atimes
Beach
Clength.out
Drepeat
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'each' repeats each element individually, not the whole vector.
Using 'length.out' tries to set the total length, not the repeat count.
2fill in blank
medium

Complete the code to add two vectors with different lengths using recycling.

R Programming
c(1, 2, 3, 4) + c(10, [1])
Drag options to blanks, or click blank then click option'
A20, 30, 40, 50
B20
C20, 30
D20, 30, 40
Attempts:
3 left
💡 Hint
Common Mistakes
Using a vector with length 1 does not show recycling clearly.
Using a vector longer than the first causes an error.
3fill in blank
hard

Fix the error in the code that tries to add vectors of incompatible lengths.

R Programming
c(1, 2, 3) + c(10, 20, 30, [1])
Drag options to blanks, or click blank then click option'
A50
B40
C60
D70
Attempts:
3 left
💡 Hint
Common Mistakes
Adding vectors of lengths that are not multiples causes warnings.
Adding vectors of different lengths without recycling causes errors.
4fill in blank
hard

Fill both blanks to create a vector that recycles elements of c(1, 2) to length 5 and then multiply by 2.

R Programming
rep(c(1, 2), [1] = 5) * [2]
Drag options to blanks, or click blank then click option'
Alength.out
Btimes
C2
Deach
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'times' instead of 'length.out' changes repetition count, not length.
Multiplying by a vector instead of a number causes unexpected results.
5fill in blank
hard

Fill all three blanks to create a named vector with lengths of words longer than 3 letters.

R Programming
lengths = { [1]: [2] for word in words if nchar(word) [3] 3 }
Drag options to blanks, or click blank then click option'
Aword
Bnchar(word)
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' filters the wrong words.
Swapping names and values causes incorrect vector structure.

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