R中指定列数以上的矩阵的行和 [英] rowsum for matrix over specified number of columns in R

查看:604
本文介绍了R中指定列数以上的矩阵的行和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取某行的R矩阵中的列总和.但是,我不希望对整个行求和,而只希望对指定数量的列求和,即在这种情况下,对角线以上的所有列.我已经尝试过sum和rowSums函数,但是它们给了我奇怪的结果或一条错误消息.为了说明,请参见下面的8x8矩阵示例代码.对于第一行,我需要除项目[1,1]之外的那一行的总和,对于第二行,我需要除项目[2,1]和[2,2]等之外的总和.

I'm trying to get the sum of columns in a matrix in R for a certain row. However, I don't want the whole row to be summed but only a specified number of columns i.e. in this case all column above the diagonal. I have tried sum and rowSums function but they are either giving me strange results or an error message. To illustrate, please see example code for an 8x8 matrix below. For the first row I need the sum of the row except item [1,1], for second row the sum except items [2,1] and [2,2] etc.

m1 <- matrix(c(0.2834803,0.6398198,0.0766999,0.0000000,0.0000000,0.0000000,0.0000000,0.0000000,
               0.0000000,0.1101746,0.6354086,0.2544168,0.0000000,0.0000000,0.0000000,0.0000000,
               0.0000000,0.0000000,0.0548145,0.9451855,0.0000000,0.0000000,0.0000000,0.0000000,
               0.0000000,0.0000000,0.0000000,0.3614786,0.6385214,0.0000000,0.0000000,0.0000000,
               0.0000000,0.0000000,0.0000000,0.0000000,0.5594658,0.4405342,0.0000000,0.0000000,
               0.0000000,0.0000000,0.0000000,0.0000000,0.0000000,0.7490395,0.2509605,0.0000000,
               0.0000000,0.0000000,0.0000000,0.0000000,0.0000000,0.0000000,0.5834363,0.4165637,
          0.0000000,0.0000000,0.0000000,0.0000000,0.0000000,0.0000000,0.0000000,1.0000000),
             8, 8, byrow = TRUE, 
             dimnames = list(c("iAAA", "iAA", "iA", "iBBB", "iBB", "iB", "iCCC", "iD"),
                  c("iAAA_p", "iAA_p", "iA_p", "iBBB_p", "iBB_p", "iB_p", "iCCC_p", "iD_p")))

我尝试了以下操作:

rowSums(m1[1, 2:8]) --> Error in rowSums(m1[1, 2:8]) : 
  'x' must be an array of at least two dimensions

或者:

sum(m1[1,2]:m1[1,8]) --> wrong result of 0.6398198 (which is item [1,2])

据我了解,rowSums需要一个数组而不是一个向量(尽管不确定为什么).但是我不明白为什么第二种使用sum的方法行不通.理想情况下,有一种方法可以只对对角线上方的一行中的所有列求和.

As I understand rowSums needs an array rather than a vector (although not sure why). But I don't understand why the second way using sum doesn't work. Ideally, there is some way to only sum all columns in a row that lie above the diagonal.

非常感谢!

推荐答案

问题是您没有将数组传递给rowSums:

The problem is you are not passing an array to rowSums:

class(m1[1,2:8])
# [1] "numeric"

这是一个数字向量.使用多个行,将可以正常工作:

This is a numeric vector. Use more than a single row and it will work just fine:

class(m1[1:2,2:8])
# [1] "matrix"

rowSums(m1[1:2,2:8])
#     iAAA       iAA 
#0.7165197 1.0000000 

如果要对位于对角线上方的所有列求和,则可以使用lower.tri将对角线以下的所有元素设置为0(或者可能是NA),然后使用rowSums.如果您不想自己包括对角线元素,则可以设置diag = TRUE(感谢@Fabio指出了这一点):

If you want to sum all the columns that lie above the diagonal then you can use lower.tri to set all elements below the diagonal to 0 (or perhaps NA) and then use rowSums. If you do not want to include the diagonal elements themselves you can set diag = TRUE (thanks to @Fabio for pointing this out):

m1[lower.tri(m1 , diag = TRUE)] <- 0
rowSums(m1)
#     iAAA       iAA        iA      iBBB       iBB        iB      iCCC        iD 
#0.7165197 0.8898254 0.9451855 0.6385214 0.4405342 0.2509605 0.4165637 0.0000000 

#  With 'NA'
m1[lower.tri(m1)] <- NA
rowSums(m1,na.rm=T)
#     iAAA       iAA        iA      iBBB       iBB        iB      iCCC        iD 
#0.7165197 0.8898254 0.9451855 0.6385214 0.4405342 0.2509605 0.4165637 0.0000000 

这篇关于R中指定列数以上的矩阵的行和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆