0
0
R Programmingprogramming~20 mins

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

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