Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic addition using R

Tags:

r

I have this matrix:

> y
     [,1] [,2] [,3] [,4]
[1,]    1    8   36  180
[2,]    5   24   21   32
[3,]    9   40    6   48
[4,]   13   56   45  180
[5,]   17   72  117   28

and this vector:

> x
[1] 10 25 34 41 59

I should SUM the elements of the row and then subtract the result with the respective value on the x vector.

Example:

10 - (1 + 8 + 36 + 180)
25 - (5 + 24 + 21 + 32)
34 - (9 + 40 + 6 + 48)
41 - (13 + 56 + 45 + 180)
59 - (17 + 72 + 117 + 28)

How can I create a vector with the results of those computations?

Thanks

like image 511
Bleak Avatar asked Jul 15 '26 07:07

Bleak


1 Answers

This should work:

x-rowSums(y)
like image 74
daroczig Avatar answered Jul 17 '26 21:07

daroczig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!