Exams Knowledge Hub

MCQs for Competitive Exams, School & College Exams

Module: | Base R Data Structures & Subsetting

Q4: Consider the following statements regarding vectorization and recycling rules in R:

1. When performing element-wise arithmetic on two vectors of unequal lengths, R automatically recycles elements of the shorter vector sequentially to match the longer vector.
2. If the length of the longer vector is not a precise multiple of the shorter vector's length, R will execute the operation but emit a warning.
3. Any arithmetic operation between a standard numeric vector (length greater than zero) and a zero-length vector results in a fatal runtime error.

Which of the above statements is/are correct?
A
Only 1
B
Only 1 and 2
C
Only 2 and 3
D
1, 2, and 3
✅ Correct Answer: B
🎯 Quick Answer:
B. Statements 1 and 2 are correct. Statement 3 is incorrect.
Concept Definition: Vectorization is the process where operations are applied element-by-element without explicit programming loops.
Recycling is the underlying mechanism R uses to mathematically align vectors of different lengths.
Structural Breakdown: In the operation c(1, 2, 3) + c(10, 20), the shorter vector is padded to become c(10, 20, 10), yielding a final output of c(11, 22, 13) alongside a warning.
Historical/Related Context: Vector recycling is a defining paradigm of the S and R languages, strongly differentiating them from strict array-based languages like C where misaligned lengths throw immediate compilation or runtime errors.
Causal Reasoning: Statement 3 is incorrect because operating against a zero-length vector in R does not trigger an error; instead, due to the recycling rule mathematically multiplying by length 0, the output is instantly collapsed to a zero-length vector of the appropriate coerced type.