0
0
R Programmingprogramming~20 mins

Vector arithmetic (element-wise) in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vector Arithmetic Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this vector addition?
Consider the following R code that adds two vectors element-wise. What is the output?
R Programming
v1 <- c(2, 4, 6)
v2 <- c(1, 3, 5)
v_sum <- v1 + v2
print(v_sum)
A[1] 3 7 9
B[1] 3 7 11
C[1] 3 7 10
D[1] 3 7 12
Attempts:
2 left
💡 Hint
Add each element of v1 to the corresponding element of v2.
Predict Output
intermediate
2:00remaining
What is the output of this vector multiplication?
What will this R code print when multiplying two vectors element-wise?
R Programming
a <- c(3, 0, 2)
b <- c(4, 5, 6)
result <- a * b
print(result)
A[1] 12 0 12
B[1] 7 5 8
C[1] 12 5 12
D[1] 3 0 2
Attempts:
2 left
💡 Hint
Multiply each element of 'a' by the corresponding element of 'b'.
🔧 Debug
advanced
2:30remaining
Why does this vector subtraction code produce a warning?
Examine the code below. Why does R give a warning when subtracting these vectors?
R Programming
x <- c(10, 20, 30, 40, 50)
y <- c(1, 2)
z <- x - y
print(z)
ABecause x and y must be the same length or R throws an error.
BBecause subtraction is not allowed between vectors in R.
CBecause y is not numeric.
DBecause vectors have different lengths and R recycles elements with a warning.
Attempts:
2 left
💡 Hint
Check the lengths of the vectors and how R handles recycling.
Predict Output
advanced
2:00remaining
What is the output of this vector division with recycling?
What will this R code print when dividing vectors of different lengths?
R Programming
v1 <- c(8, 16, 24, 32, 40)
v2 <- c(2, 4)
result <- v1 / v2
print(result)
A[1] 4 4 12 8 20
B[1] 4 4 12 8 20 # no warning
C[1] 4 4 12 8 20 # with warning
D[1] 4 4 6 8 10
Attempts:
2 left
💡 Hint
Remember how R recycles the shorter vector to match the longer one.
Predict Output
expert
3:00remaining
What is the value of 'result' after this complex vector operation?
Given the code below, what is the value of the variable 'result'?
R Programming
v <- c(1, 2, 3, 4, 5)
result <- (v * 2) - (v %% 2) + c(5, 4, 3, 2, 1)
print(result)
A[1] 6 8 8 10 10
B[1] 6 8 9 10 11
C[1] 6 7 9 10 11
D[1] 6 7 8 10 11
Attempts:
2 left
💡 Hint
Calculate each part step-by-step: multiply, modulo, then add.