0
0
R Programmingprogramming~10 mins

Vector recycling behavior in R Programming - Interactive Code Practice

Choose your learning style9 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.