NoName
NoName

Reputation: 10324

Sum of Multiple Vectors

So the behavior of the following code:

x =  c(1, 2, 3, 4, 5) 
print(sum(x - 3))

is enter image description here

But what is the behavior of using multiple vectors in a sum?

For example:

print(sum(x - y - 3))
print(sum(x - y - z - 3))

Upvotes: 2

Views: 275

Answers (1)

ThomasIsCoding
ThomasIsCoding

Reputation: 101343

Let's think like this way:

For

sum(v1 - v2 - 3)

it is equivalent to

v <- v1 - v2
sum(v - 3)

where v is a vector from the element-wise difference between v1 and v2, and sum(v-3) is the same as what you did to sum(vector - 3) in your post.


Remark

If you have unequal lengths between multiple vectors, the recycling rule will be applied (see comment by @Waldi)

Upvotes: 3

Related Questions