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

Number of rows with exactly 1 numerical value in matrice (R)

I have a matrice that is as such:

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
 [1,]    0    0    0    0    0    0    0    0    0
 [2,]    0    0    0    0    0    0    0    0    0
 [3,]    0    0    0    0    0    0    0    0    0
 [4,]    0    0    0    0    0    0    0    1    1
 [5,]    0    0    0    0    0    0    1    1    0
 [6,]    0    0    0    0    0    1    0    0    0
 [7,]    0    0    0    0    1    1    0    0    0
 [8,]    0    0    0    0    1    0    0    0    0
 [9,]    0    0    0    0    1    0    0    0    0
[10,]    0    0    0    0    1    1    0    0    0
[11,]    0    0    0    0    0    1    0    0    0
[12,]    0    0    0    0    0    1    1    1    1
[13,]    0    0    0    0    0    0    0    0    0
[14,]    0    0    0    0    0    0    0    0    0
[15,]    0    0    0    0    0    0    0    0    0
[16,]    0    0    0    0    0    0    0    0    0
[17,]    0    0    0    0    0    0    0    0    0
      [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17]
 [1,]     0     0     0     0     0     0     0     0
 [2,]     0     0     0     0     0     0     0     0
 [3,]     0     0     0     0     0     0     0     0
 [4,]     1     1     0     0     0     0     0     0
 [5,]     0     1     0     0     0     0     0     0
 [6,]     0     1     0     0     0     0     0     0
 [7,]     0     1     0     0     0     0     0     0
 [8,]     0     1     0     0     0     1     0     0
 [9,]     0     1     0     0     0     1     0     0
[10,]     1     1     0     0     0     1     0     0
[11,]     1     1     0     1     1     0     0     0
[12,]     1     1     1     1     0     0     0     0
[13,]     0     0     0     0     0     0     0     0
[14,]     0     0     0     0     0     0     0     0
[15,]     0     0     0     0     0     0     0     0
[16,]     0     0     0     0     0     0     0     0
[17,]     0     0     0     0     0     0     0     0
      [,18]
 [1,]     0
 [2,]     0
 [3,]     0
 [4,]     0
 [5,]     0
 [6,]     0
 [7,]     0
 [8,]     0
 [9,]     0
[10,]     0
[11,]     0
[12,]     0
[13,]     0
[14,]     0
[15,]     0
[16,]     0
[17,]     0

How can I count the number of rows with exactly 1 value, not more than one?

I’ve tried using nrow(imageMatrix[imageMatrix < 2])
and also tried converting the matrice to dataframe and then using nrow(dataframe_matrice[dataframe_matrice == 1,])

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

but it has been of no avail.

Here imageMatrix is the name of the matrice.

Can someone please offer me a hint on what I’m doing wrong with my first line of code in counting rows?

>Solution :

We may use rowSums on a logical matrix (imageMatrix == 1) and then create a logical vector == 1 and get the count with sum

sum(rowSums(imageMatrix == 1) == 1)

imageMatrix <2 is a logical matrix, when it is used to subset the original matrix, it returns a vector of values which doesn’t have dim and thus nrow wouldn’t work i.e.

nrow(1:5)
NULL
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