It means performing operations like addition, subtraction, multiplication, or division on each pair of elements from two vectors of the same length, one by one.
Click to reveal answer
intermediate
How does R handle vector addition when vectors have different lengths?
R recycles the shorter vector to match the length of the longer one, repeating its elements until lengths match, but it gives a warning if the longer vector's length is not a multiple of the shorter one.
Click to reveal answer
beginner
What will be the result of c(1, 2, 3) + c(4, 5, 6) in R?
A new vector where each element is the sum of elements at the same position: c(5, 7, 9).
Click to reveal answer
beginner
Explain element-wise multiplication of vectors in R with an example.
Element-wise multiplication multiplies each element of one vector by the corresponding element of another vector. For example, c(2, 3, 4) * c(5, 6, 7) results in c(10, 18, 28).
Click to reveal answer
intermediate
What happens if you perform element-wise division with vectors of different lengths in R?
R recycles the shorter vector to match the longer one and divides elements pairwise, but warns if lengths are not multiples. For example, c(10, 20, 30, 40) / c(2, 5) results in c(5, 4, 15, 8).
Click to reveal answer
What does element-wise addition of vectors mean in R?
AAdding the first element of one vector to all elements of the other
BAdding all elements of both vectors together
CAdding each element of one vector to the corresponding element of another vector
DConcatenating two vectors
✗ Incorrect
Element-wise addition adds elements at the same positions in two vectors.
What will R do if you add vectors of different lengths?
AIgnore the shorter vector
BReturn an error and stop
COnly add elements up to the length of the shorter vector
DRecycle the shorter vector to match the longer one
✗ Incorrect
R recycles the shorter vector elements to match the longer vector length.
What is the result of c(3, 6, 9) * c(2, 4, 6) in R?
Ac(6, 24, 54)
Bc(1, 2, 3)
Cc(5, 10, 15)
Dc(3, 6, 9, 2, 4, 6)
✗ Incorrect
Element-wise multiplication multiplies each pair: 3*2=6, 6*4=24, 9*6=54.
If you divide c(8, 16, 24) by c(2, 4), what will happen?
AError due to different lengths
BResult c(4, 4, 12) with a warning
CResult c(4, 4, 6) with a warning
DResult c(4, 4, 12) without warning
✗ Incorrect
Shorter vector c(2,4) is recycled to c(2,4,2). Division: 8/2=4, 16/4=4, 24/2=12 with a warning.
Which operator is used for element-wise subtraction in R?
A-
B+
C*
D/
✗ Incorrect
The minus sign (-) subtracts elements pairwise in vectors.
Describe how R performs element-wise arithmetic operations on vectors and what happens when vectors have different lengths.
Think about how R matches elements by position and repeats shorter vectors.
You got /4 concepts.
Give examples of element-wise addition, multiplication, and division of vectors in R, explaining the results.
Use simple vectors like c(1,2,3) and c(4,5,6) to illustrate.
You got /4 concepts.
Practice
(1/5)
1.
What happens when you add two vectors of the same length in R, like c(1, 2, 3) + c(4, 5, 6)?
easy
A. The vectors are concatenated into one longer vector.
B. Only the first elements are added, the rest are ignored.
C. Each element in the first vector is added to the corresponding element in the second vector.
D. An error occurs because vectors cannot be added.
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
Adding c(1, 2, 3) and c(4, 5, 6) results in c(1+4, 2+5, 3+6) which is c(5, 7, 9).
Final Answer:
Each element in the first vector is added to the corresponding element in the second vector. -> Option C
Quick Check:
Element-wise addition = Each element in the first vector is added to the corresponding element in the second vector. [OK]
Hint: Add vectors element by element when lengths match [OK]
Common Mistakes:
Thinking vectors concatenate instead of adding
Assuming only first elements add
Expecting an error for vector addition
2.
Which of the following is the correct syntax to subtract two vectors a and b element-wise in R?
easy
A. subtract_vectors(a, b)
B. subtract(a, b)
C. a %minus% b
D. a - b
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
Only a - b is valid R syntax for element-wise subtraction. The other options are not built-in R functions or operators.
Final Answer:
a - b -> Option D
Quick Check:
Use - operator for vector subtraction [OK]
Hint: Use '-' operator directly between vectors [OK]
A. Vectors have different lengths causing recycling warning.
B. Syntax error due to missing operator.
C. Result will be NULL because vectors differ in length.
D. No issue; code runs correctly without warnings.
Solution
Step 1: Check vector lengths
Vector v1 has length 3, v2 has 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 A
Quick Check:
Unequal vector lengths cause recycling warning [OK]
Hint: Vectors must be same length or multiples to avoid warnings [OK]
Common Mistakes:
Ignoring recycling warnings
Expecting syntax errors for length mismatch
Assuming result is NULL when lengths differ
5.
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?
hard
A. total_sales <- store1 + store2
B. total_sales <- store1 * store2
C. total_sales <- store1 - store2
D. total_sales <- c(store1, store2)
Solution
Step 1: Understand the goal
We want total sales per day, so we add sales from both stores for each day.