R - Loop through different matrices without using a loop! Help just create code

So, I have two separate matrices (mat1 and mat2) and I need to go through them to check. I need to save the results in the third matrix.

I feel my code is very long for this purpose.

I wanted some of your suggestions to avoid the loop.

So my first matrix looks like this (dput at 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 - to save the test results.

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

Here is my loop - 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] <- '!'} } } 

Result of checking

  [,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"))) 
+5
source share
1 answer

In the above example, the result can be constructed as

 matCheck <- ( mat1 | !mat2 ) 

This is equivalent to initializing matCheck true and then populating false, where !mat1 & mat2 (as in the OP loop). Brackets are optional but make reading easier (I think).

+4
source

All Articles