5.2 More Subtle Vector Math
What happens when things are not simple?
Suppose you try to add c(1, 2, 3, 4) + c(5, 6). What is the result? In a matrix algebra class, you would say that this answer is undefined. If we try it in R, we actually get an answer.
c(1, 2, 3, 4) + c(5, 6)
Essentially, if the vectors are not of the same length, the shorter one is duplicated sufficiently to allow the operation to proceed. In this case, the answer reflects an adjustment of the question to:
c(1, 2, 3, 4) + c(5, 6, 5, 6)
And we can do a similar example with multiplication and obtain a similar answer.
c(1, 2, 3, 4)*c(5, 6)
But, this kind of math does not work in all cases. For example, if we slightly changed the problem to c(1, 2, 3) + c(5, 6), then an error would be produced.
c(1, 2, 3) + c(5, 6)
As the error states, the smaller vector needs to be a multiple of the larger vector. The first example satisfied the criterion, but in this case it did not.