R - Check different matrices with a possible lag

60 views Asked by At

This issue is quite tricky to explain but I am sure some of you already faced it.

So I have two matrix. Matrix 1 (mat 1) and Matrix 2 (mat 2)

What I want to do is to record in a third matrix (mat3) the value of mat2, after checking for matrix 1, but with a LAG. Let me explain.

After the value 1 in matrix 1, I want to check if matrix 2 as a 1 too but within the range of a certain LAG, for example, 1 or 2 episodes after (column).

For example, row number 4 has a 1 in matrix 1 at the 6th column. So I want to check if in matrix 2 for row number 4 it has a 1 directly after or after 2 or 3 more columns.

Do you understand the idea ?

mat1 = structure(c(0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 
0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 
0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 
1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 
0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 
0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1), .Dim = c(10L, 21L), .Dimnames = list(NULL, c("wit5.020", 
"wit5.021", "wit5.022", "wit5.023", "wit5.024", "wit5.025", "wit5.026", 
"wit5.027", "wit5.028", "wit5.029", "wit5.030", "wit5.031", "wit5.032", 
"wit5.033", "wit5.034", "wit5.035", "wit5.036", "wit5.037", "wit5.038", 
"wit5.039", "wit5.040")))

mat2 = structure(c(0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 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, 1, 0, 0, 
0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 
0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 
1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 
0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 
1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 
0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 
0, 1, 0, 1), .Dim = c(10L, 21L))

So mat3 - where I want to store the result of the check

mat3 = matrix(0, nrow = nrow(mat1), ncol = ncol(mat1))

So here is an example of a possible loop in order to check the LAG - this loop doesn't work but it could give you an idea maybe of the solution. I am not sure where to introduce the lag. I thought maybe in the i, but I am not sure.

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

    if( mat1[i,j] == 1 & mat2[i,j] == 1 | mat2[i+1,j] == 1 | mat2[i+2,j] == 1) # lag here 
    {mat[i,j] <- 1}  

else
{mat[i,j] <- 0}  

  }
}

Any ideas are very welcome.

1

There are 1 answers

1
Dagremu On BEST ANSWER

Here's a simple way to do it:

lag <- 3  # or whatever lag you want
nr <- nrow(mat1)
nc <- ncol(mat1)

mat3 <- matrix(0, ncol=nc, nrow=nr)
for (r in 1:nr) {
  for (c in 1:nc) {
    if (mat1[r,c] == 1 && any(mat2[r,c:min(c+lag,nc)] == 1))
      mat3[r,c] <- 1
  }
}

Note the use of mat2[r,c:min(c+lag,nc)]. This selects all elements from current column c up through column c + lag, but it makes sure not to go past nc (the total number of columns). That is, this code is used to avoid an out-of-bounds error.

There's probably a faster, more vectory way of doing this, but the above code should work.