0
0
R Programmingprogramming~5 mins

Vector arithmetic (element-wise) in R Programming - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is vector arithmetic (element-wise) in R?
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
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
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)
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
Which operator is used for element-wise subtraction in R?
A-
B+
C*
D/
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.