开发者

Automatic addition using R

开发者 https://www.devze.com 2023-03-23 21:36 出处:网络
I have this matrix: > y [,1] [,2] [,3] [,4] [1,]1836180 [2,]5242132 [3,]940648 [4,]135645180 [5,]177211728

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 ve开发者_开发技巧ctor.

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


This should work:

x-rowSums(y)
0

精彩评论

暂无评论...
验证码 换一张
取 消