将函数应用于具有相似名称的多个矩阵

时间:2017-07-21 19:36:07

标签: r for-loop matrix sum assign

我在全球环境中保存了1864个矩阵(7 x 7维),称为“probability_matrix [i]”,例如:probability_matrix1,probability_matrix2等......

我使用以下方法生成了这些矩阵:

    for (i in 1:length(lambda.p)){
assign(
paste("probability_matrix", i, sep = ""), 
dpois(0:maxgoal, lambda.p[i]) %*% t(dpois(0:maxgoal, mu.p[i]))
    )
}

现在我试图总结矩阵的一些元素,并将它们保存为行向量。我可以为每个人编写它,但我正在尝试使用“for循环”。

HomeWinProbability1 <- sum(probability_matrix1[lower.tri(probability_matrix1)])
HomeWinProbability2 <- sum(probability_matrix2[lower.tri(probability_matrix2)])
HomeWinProbability <- rbind(HomeWinProbability1,HomeWinProbability2)

我试过了:

for (i in 1:length(lambda.p)){
assign(
paste("HomeWinProbability", i, sep = ""), 
sum(paste("probability_matrix", i, sep = "")[lower.tri(paste("probability_matrix", i, sep = ""))])
      )
} 

但是我收到以下错误消息:

Error in sum(paste("probability_matrix", i, sep = "")[lower.tri(paste("probability_matrix",  : 
invalid 'type' (character) of argument

你会怎么做?非常感谢。

0 个答案:

没有答案