R: How to sum pairs in a Matrix by row?

前端 未结 5 736
臣服心动
臣服心动 2021-01-23 02:54

Probably this would be easy. I have a Matrix:

testM <- matrix(1:40, ncol = 4, byrow = FALSE)
testM
      [,1] [,2] [,3] [,4]
 [1,]    1   11   21   31
 [2,]           


        
5条回答
  •  太阳男子
    2021-01-23 03:24

    testM[,c(T,F)]+testM[,c(F,T)];
    ##       [,1] [,2]
    ##  [1,]   12   52
    ##  [2,]   14   54
    ##  [3,]   16   56
    ##  [4,]   18   58
    ##  [5,]   20   60
    ##  [6,]   22   62
    ##  [7,]   24   64
    ##  [8,]   26   66
    ##  [9,]   28   68
    ## [10,]   30   70
    

提交回复
热议问题