R - Loop through different matrices without using loop ! Help to simply a code

100 views Asked by At

So I have two separate matrix (mat1 and mat2) and I need to go through them in order to make a check. I need to store the results into a third matrix.

I feel that my code is very long for the purpose.

I wanted to have some of your suggestion to avoid looping.

So my first matrix looks like this (dput in the end)

  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

My second matrix has a similar structure.

Here I create my third matrix - in order to store the results of the checking.

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

Here is my loop - that I would like to avoid

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] <- '!'}  

      }
   }

The result of the check

      [,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" 

Any suggestions ?

Here is matrix 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")))

Here is matrix 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

There are 1 answers

0
Frank On BEST ANSWER

For the example given, the result can be constructed as

matCheck <- ( mat1 | !mat2 )

This is equivalent to initializing matCheck to true and then filling in false where !mat1 & mat2 (as in the OP's loop). The parentheses are optional, but make it easier to read (I think).