特定の行について、R の行列の列の合計を取得しようとしています。ただし、行全体を合計するのではなく、指定された数の列のみ、つまりこの場合は対角線の上のすべての列を合計したくありません。sum 関数と rowSums 関数を試しましたが、奇妙な結果が表示されるか、エラー メッセージが表示されます。説明のために、以下の 8x8 マトリックスのコード例を参照してください。最初の行では、アイテム [1,1] を除く行の合計が必要です。2 行目では、アイテム [2,1] と [2,2] を除く合計が必要です。
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にはベクトルではなく配列が必要です(理由はわかりませんが)。しかし、合計を使用する2番目の方法が機能しない理由がわかりません。理想的には、対角線の上にある行のすべての列のみを合計する何らかの方法があります。
どうもありがとう!