Vector arithmetic lets you do math on lists of numbers all at once, making calculations faster and easier.
Vector arithmetic (element-wise) in R Programming
result <- vector1 + vector2 result <- vector1 - vector2 result <- vector1 * vector2 result <- vector1 / vector2
Vectors must be the same length or R will recycle elements, which can cause unexpected results.
Operations are done element by element, like adding each pair of numbers from the two vectors.
a <- c(1, 2, 3) b <- c(4, 5, 6) sum <- a + b
a <- c(10, 20, 30) b <- c(1, 2, 3) diff <- a - b
a <- c(2, 4, 6) b <- c(3, 5, 7) product <- a * b
a <- c(8, 16, 24) b <- c(2, 4, 8) quotient <- a / b
This program adds temperatures from two cities day by day to find the combined temperature.
temps_city1 <- c(20, 22, 19, 24) temps_city2 <- c(18, 21, 20, 23) total_temps <- temps_city1 + temps_city2 print(total_temps)
If vectors have different lengths, R repeats the shorter one to match the longer, which can cause mistakes.
Use functions like length() to check vector sizes before doing arithmetic.
Vector arithmetic applies math operations to each pair of elements in two vectors.
Vectors should be the same length to avoid unexpected recycling.
This makes working with lists of numbers easy and fast.
Practice
What happens when you add two vectors of the same length in R, like c(1, 2, 3) + c(4, 5, 6)?
Solution
Step 1: Understand element-wise addition
In R, adding two vectors of the same length adds each element from the first vector to the matching element in the second vector.Step 2: Apply addition to given vectors
Addingc(1, 2, 3)andc(4, 5, 6)results inc(1+4, 2+5, 3+6)which isc(5, 7, 9).Final Answer:
Each element in the first vector is added to the corresponding element in the second vector. -> Option CQuick Check:
Element-wise addition = Each element in the first vector is added to the corresponding element in the second vector. [OK]
- Thinking vectors concatenate instead of adding
- Assuming only first elements add
- Expecting an error for vector addition
Which of the following is the correct syntax to subtract two vectors a and b element-wise in R?
Solution
Step 1: Recall vector subtraction syntax in R
In R, element-wise subtraction of vectors uses the minus operator-directly between vectors.Step 2: Verify given options
Onlya - bis valid R syntax for element-wise subtraction. The other options are not built-in R functions or operators.Final Answer:
a - b -> Option DQuick Check:
Use-operator for vector subtraction [OK]
- Using non-existent functions like subtract()
- Trying special operators like %minus%
- Confusing subtraction with other functions
What is the output of the following R code?
v1 <- c(2, 4, 6)
v2 <- c(1, 3, 5)
result <- v1 * v2
print(result)
Solution
Step 1: Understand element-wise multiplication
In R, multiplying two vectors multiplies each element of the first vector by the corresponding element of the second vector.Step 2: Calculate each product
Multiply elements: 2*1=2, 4*3=12, 6*5=30. So the result isc(2, 12, 30).Final Answer:
[1] 2 12 30 -> Option BQuick Check:
Element-wise multiplication = [1] 2 12 30 [OK]
- Adding instead of multiplying
- Concatenating vectors instead of multiplying
- Getting sums instead of products
Consider this R code snippet:
v1 <- c(1, 2, 3)
v2 <- c(4, 5)
result <- v1 + v2
print(result)
What is the issue with this code?
Solution
Step 1: Check vector lengths
Vectorv1has length 3,v2has length 2. They differ in length.Step 2: Understand recycling in R
R recycles the shorter vector to match the longer one but gives a warning if lengths are not multiples. Here, 3 is not a multiple of 2, so a warning occurs.Final Answer:
Vectors have different lengths causing recycling warning. -> Option AQuick Check:
Unequal vector lengths cause recycling warning [OK]
- Ignoring recycling warnings
- Expecting syntax errors for length mismatch
- Assuming result is NULL when lengths differ
You have two vectors representing daily sales for two stores:store1 <- c(10, 20, 30, 40)store2 <- c(5, 15, 25, 35)
How do you calculate the total sales per day for both stores combined using vector arithmetic?
Solution
Step 1: Understand the goal
We want total sales per day, so we add sales from both stores for each day.Step 2: Use element-wise addition
Addingstore1andstore2element-wise sums daily sales:c(10+5, 20+15, 30+25, 40+35)=c(15, 35, 55, 75).Final Answer:
total_sales <- store1 + store2 -> Option AQuick Check:
Add vectors element-wise for daily totals [OK]
- Multiplying instead of adding sales
- Subtracting sales mistakenly
- Concatenating vectors instead of adding
Ready to prove it?
Pass the test and this pattern moves unknown
