Home > front end >  Number of rows with exactly 1 numerical value in matrice (R)
Number of rows with exactly 1 numerical value in matrice (R)

Time:03-15

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,])

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?

CodePudding user response:

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
  •  Tags:  
  • r
  • Related