📖 Scenario: Imagine you are working with two sets of numbers representing daily sales and daily targets for a small shop. Sometimes, the number of sales days and target days don't match. R helps by repeating the shorter vector to match the longer one. This is called vector recycling.
🎯 Goal: You will create two vectors with different lengths, set a variable for the length of the longer vector, use vector recycling to add the sales and targets, and finally print the result.
📋 What You'll Learn
Create a numeric vector called sales with values 10, 20, 30, 40
Create a numeric vector called targets with values 5, 15
Create a variable called length_long that stores the length of the longer vector between sales and targets
Create a vector called total that adds sales and targets using vector recycling
Print the total vector
💡 Why This Matters
🌍 Real World
Vector recycling helps when working with data sets of different lengths, like sales data and targets, making calculations easier without manual repetition.
💼 Career
Understanding vector recycling is important for data analysis and programming in R, common in data science and statistics jobs.
Progress0 / 4 steps
1
Create the sales and targets vectors
Create a numeric vector called sales with values 10, 20, 30, 40 and a numeric vector called targets with values 5, 15.
R Programming
Hint
Use the c() function to create vectors in R.
2
Find the length of the longer vector
Create a variable called length_long that stores the length of the longer vector between sales and targets using the max() and length() functions.
R Programming
Hint
Use length() to get vector length and max() to find the bigger number.
3
Add sales and targets using vector recycling
Create a vector called total that adds sales and targets using vector recycling. Use the rep() function to repeat the shorter vector to match length_long.
R Programming
Hint
Use rep(targets, length.out = length_long) to repeat targets to the needed length.
4
Print the total vector
Print the total vector using the print() function.
R Programming
Hint
Use print(total) to show the result.
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
Step 1: Understand vector lengths
The first vector has length 3, the second has length 2.
Step 2: Apply recycling rule
The shorter vector c(4, 5) is repeated to become c(4, 5, 4) to match length 3.
Final Answer:
The shorter vector is repeated to match the longer vector's length before addition. -> Option C
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
Step 1: Identify vector addition syntax in R
In R, adding vectors element-wise is done with the + operator.
Step 2: Check other options
add() is not a base R function; & is logical AND; sum() sums all elements, not element-wise.
Final Answer:
a + b -> Option A
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
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
Step 1: Check vector lengths
x length is 3, y length is 4.
Step 2: Understand recycling warning
3 does not divide 4 evenly, so R will recycle x but give a warning about length mismatch.
Step 3: Fix the problem
Make vector lengths compatible, e.g., use length 6 or 12, or truncate longer vector.
Final Answer:
Lengths differ and 3 does not divide 4; fix by making lengths compatible. -> Option A
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
Step 1: Understand desired output length
You want a length 7 vector, so both vectors must be length 7.
Step 2: Use rep() with length.out
Using rep(v1, length.out=7) repeats v1 to length 7; similarly for v2.
Step 3: Multiply element-wise
Multiplying these repeated vectors gives length 7 without warnings.
Final Answer:
rep(v1, length.out=7) * rep(v2, length.out=7) -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.