R - 在不使用循环的情况下循环遍历不同的矩阵!帮助简单的代码

时间:2015-06-19 16:04:51

标签: r loops matrix tapply

所以我有两个单独的矩阵(mat1和mat2) 我需要通过它们才能进行检查。 我需要将结果存储到第三个矩阵中。

我觉得我的代码很长。

我想提出一些避免循环的建议。

所以我的第一个矩阵看起来像这样(最后输出)

  wit5.001 wit5.002 wit5.003 wit5.004 wit5.005 wit5.006 wit5.007 wit5.008 wit5.009 wit5.010
 [1,]        1        1        1        1        1        1        1            1        1        1
 [2,]        1        1        1        1        1        1        1        1        1        1
 [3,]        1        1        1        1        1        1        1        1        1        1
 [4,]        1        1        1        1        1        1        1        1        1        1
 [5,]        1        1        1        1        1        1        1        0        1        1
 [6,]        1        1        1        1        1        1        1        0        0        0
 [7,]        0        1        1        1        1        1        1        1        1        1
 [8,]        1        1        1        1        1        1        1        1        1        1
 [9,]        1        1        1        1        1        1        1        1        1        1
[10,]        1        1        1        1        1        1        1        1        1        1

我的第二个矩阵具有类似的结构。

这里我创建了我的第三个矩阵 - 以便存储检查结果。

matCheck <- matrix('ok', ncol = ncol(mat1), nrow = nrow(mat1))

这是我的循环 - 我想避免

for(j in 1:ncol(mat1)){
  for(i in 1:nrow(mat1)){

    if(mat1[i,j] == 1 & mat2[i,j] == 1) 
    {matCheck[i,j] <- 'ok'}  

    if(mat1[i,j] != 1 & mat2[i,j] == 1) 
    {matCheck[i,j] <- '!'}  

      }
   }

检查结果

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,] "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" 
 [2,] "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" 
 [3,] "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" 
 [4,] "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" 
 [5,] "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" 
 [6,] "ok" "ok" "ok" "ok" "ok" "ok" "ok" "!"  "!"  "ok" 
 [7,] "!"  "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" 
 [8,] "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" 
 [9,] "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" 
[10,] "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" "ok" 

有什么建议吗?

这是矩阵1

mat1 = structure(c(1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 
 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1), .Dim = c(10L, 
 10L), .Dimnames = list(NULL, c("wit5.001", "wit5.002", "wit5.003", 
"wit5.004", "wit5.005", "wit5.006", "wit5.007", "wit5.008", "wit5.009", 
"wit5.010")))

这是矩阵2

mat2 = structure(c(1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 
1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 
1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 
0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 
0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0), .Dim = c(10L, 
10L), .Dimnames = list(NULL, c("wit5.020", "wit5.021", "wit5.022", 
"wit5.023", "wit5.024", "wit5.025", "wit5.026", "wit5.027", "wit5.028", 
"wit5.029")))

1 个答案:

答案 0 :(得分:4)

对于给出的示例,结果可以构造为

matCheck

这相当于将!mat1 & mat2初始化为true,然后在{{1}}处填充false(如在OP的循环中)。括号是可选的,但更容易阅读(我认为)。