Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to set all rows of a list of matrices to zero using if condition statement in R

Suppose I have a matrix, mat. Suppose further that the sum of one row of this matrix is equal to zero. Then, I need to set all the coming rows (the rows after the zero row) to zero. For example,

     mat <- c(1,2,0,0,0,
         3,4,0,2,1,
         0,0,0,1,0,
         1,2,0,0,0,
         0,1,0,1,0)
mat <- matrix(mat,5,5)
mat


      [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    0    1    0
[2,]    2    4    0    2    1
[3,]    0    0    0    0    0
[4,]    0    2    1    0    1
[5,]    0    1    0    0    0

All the entries of row 3 are zero. Hence, I want rows 4, and 5 to become zeros as well. I have a list of matrices and would like to apply the same to all the matrices using the lapply function. For simplicity, I make a list of 3 matrices similar to the mat.

mat <- c(1,2,0,0,0,
         3,3,0,2,1,
         0,0,0,4,0,
         1,3,0,0,0,
         0,1,0,1,0)
mat <- matrix(mat,5,5)
mat1 <- c(1,2,0,0,0,
         3,4,0,2,1,
         0,0,0,1,0,
         1,2,0,0,0,
         0,1,0,1,0)
mat1 <- matrix(mat1,5,5)
mat2 <- c(1,2,0,0,0,
         3,4,0,2,1,
         0,0,0,2,0,
         1,2,0,0,0,
         0,2,0,3,0)
mat2 <- matrix(mat2,5,5)
Mat <- list(mat1, mat2, mat3)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You did not actually post mat3 in your data so I just used mat3 <- matrix(1, 5, 5), i.e. a 5×5 matrix of ones. This was to ensure it could handle cases where there is no row where all values are zero.

This will return a list of matrices where all rows are zero after the first row of zeroes:

lapply(Mat, \(mat) {
    first_zero_row  <- which(rowSums(mat)==0)[1]
    if(!is.na(first_zero_row)) {
        mat[first_zero_row:nrow(mat),]  <- 0    
    }
    mat
})

Output:

[[1]]
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    0    1    0
[2,]    2    4    0    2    1
[3,]    0    0    0    0    0
[4,]    0    0    0    0    0
[5,]    0    0    0    0    0

[[2]]
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    0    1    0
[2,]    2    4    0    2    2
[3,]    0    0    0    0    0
[4,]    0    0    0    0    0
[5,]    0    0    0    0    0

[[3]]
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    1    1    1    1
[2,]    1    1    1    1    1
[3,]    1    1    1    1    1
[4,]    1    1    1    1    1
[5,]    1    1    1    1    1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading