Count the Number of Non-Zero Elements of Each Column

Count the number of non-zero elements of each column

What about:

apply(your.matrix, 2, function(c)sum(c!=0))

Does this help?

edit:

Even better:

colSums(your.matrix != 0)

edit 2:

Here we go, with an example for ya:

> example = matrix(sample(c(0,0,0,100),size=70,replace=T),ncol=7)
> example
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] 0 100 0 0 100 0 100
[2,] 100 0 0 0 0 0 100
[3,] 0 0 0 0 0 0 100
[4,] 0 100 0 0 0 0 0
[5,] 0 0 100 100 0 0 0
[6,] 0 0 0 100 0 0 0
[7,] 0 100 100 0 0 0 0
[8,] 100 0 0 0 0 0 0
[9,] 100 100 0 0 100 0 0
[10,] 0 0 0 0 0 100 0
> colSums(example != 0)
[1] 3 4 2 2 2 1 3

(new example, the previous example with '1' values was not suited to show that we are summing the number of cells, not their contents)

Count the number of non-zero elements of each column- Piping Friendly

example %>% summarise(across(where(is.numeric), ~sum(. != 0)))

count nonzero values in each column tidyverse

A possible solution:

library(dplyr)

df %>%
summarise(across(starts_with("site"), ~ sum(.x != 0)))

#> # A tibble: 1 × 3
#> site1 site2 site3
#> <int> <int> <int>
#> 1 2 3 1

Another possible solution, in base R:

apply(df[-1], 2, \(x) sum(x != 0))

#> site1 site2 site3
#> 2 3 1

calculating count of non zero values in row in dataframe

You can use the regex 'T_1_[1-9]' to exclude T_1_0.

library(dplyr)
df <- df %>% mutate(new1 = rowSums(select(., matches('T_1_[1-9]')) !=0))
df
# T_1_1 T_1_2 T_1_3 S_2_1 S_2_2 S_2_3 T_1_0 new1
#1 68 26 93 69 87 150 79 3
#2 24 105 32 67 67 0 0 3
#3 0 0 73 94 47 132 0 1
#4 105 73 103 0 120 121 115 3
#5 58 39 149 77 85 29 98 3
#6 0 97 0 136 122 78 12 1
#7 135 46 147 137 0 109 15 3
#8 126 108 113 92 96 40 121 3
#9 24 0 139 73 79 0 2 2

Or more specific :

df <- df %>% mutate(new1 = rowSums(select(., starts_with('T_1'), -T_1_0) !=0))

Count the number of column having non zero values

SELECT COUNT(*) AS Total,
...
...
CASE ( WHEN SUM(CASE WHEN TypeId ='4' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END) +
CASE ( WHEN SUM(CASE WHEN TypeId ='6' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END) +
CASE ( WHEN SUM(CASE WHEN TypeId ='1' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END) +
CASE ( WHEN SUM(CASE WHEN TypeId ='10' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END) +
CASE ( WHEN SUM(CASE WHEN TypeId ='5' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END) +
CASE ( WHEN SUM(CASE WHEN TypeId ='8' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END)
as SumOfNonZeros

FROM [Party]

Or maybe simpler

SELECT COUNT(*) AS Total,
COUNT(CASE WHEN TypeId ='4' THEN 1 END) AS 'TotalCount1',
COUNT(CASE WHEN TypeId ='6' THEN 1 END) AS 'TotalCount2',
COUNT(CASE WHEN TypeId ='1' THEN 1 END) AS 'TotalCount3',
COUNT(CASE WHEN TypeId ='10' THEN 1 END) AS 'TotalCount4',
COUNT(CASE WHEN TypeId ='5' THEN 1 END) AS 'TotalCount5',
COUNT(CASE WHEN TypeId ='8' THEN 1 END) AS 'TotalCount6',
COUNT( DISTINCT CASE WHEN TypeId IN ('4', '6', '1', '10', '5', '8')
THEN TypeId
END ) as CountOfNonZeros
FROM [Party]

Count non-zero entries in each column of a matrix

It's simply

> A ~= 0
ans =

1 1 1 1 1
1 1 1 1 1
0 0 1 0 1

> sum(A ~= 0, 1)
ans =

2 2 3 2 3

Count non zero entry in row in R

In base R

df$count <- rowSums(df!=0)
mode1 mode2 mode3 count
1 8 1 0 2
2 0 0 0 0
3 6 5 4 3
4 1 2 3 3
5 1 1 1 3

Using dplyr

library(dplyr)
df %>% mutate(count=rowSums(.!=0))
#For specific columns we can use
df %>% mutate(count=rowSums(.[1:3]!=0))
#OR
df %>% mutate(count=rowSums(select(.,starts_with("mode"))!=0))

Count the number of non-zero columns in a matrix

If you want the columns that have non-zero elements, meaning they may have zeros but not all the rows are zeros then the following works:

mat<-matrix(rep(0,2*5),ncol=2)
mat[,1]=c(1,2,3,4,5)

sum(colSums(mat!=0)!=0)
#> [1] 1

But if you want to find the column which have no zeros, look below. I am making an example to illustrate that better:

mat2<-matrix(rep(0,4*5),ncol=4)
mat2[,1]=c(0,2,3,4,5)
mat2[,2]=c(1,2,3,4,5)
mat2[,3]=c(0,0,0,-1,1)

sum(colSums(mat2!=0)!=0) #count of non-zero columns
#> [1] 3

sum(colSums(mat2!=0)==nrow(mat2)) #count of columns with no zeros
#> [1] 1

If you want to find out which columns are non-zero then use which instead of sum:

which(colSums(mat2!=0)!=0) #non-zero columns
#> [1] 1 2 3

which(colSums(mat2!=0)==nrow(mat2)) #columns with no zeros
#> [1] 2


Related Topics



Leave a reply



Submit