pachadotdev
pachadotdev

Reputation: 3765

How to count zeros in each column using dplyr?

I want to count zeros in a dataframe.

To count NAs I'm using

mtcars %>% group_by(cyl) %>% summarise_each(funs(sum(is.na(.))))

that returns

# A tibble: 3 × 11
    cyl   mpg  disp    hp  drat    wt  qsec    vs    am  gear  carb
  <dbl> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
1     4     0     0     0     0     0     0     0     0     0     0
2     6     0     0     0     0     0     0     0     0     0     0
3     8     0     0     0     0     0     0     0     0     0     0

How can I do something like

mtcars %>% group_by(cyl) %>% summarise_each(funs(sum(identical(.,0)))

to achieve the same result but counting zeros instead of NAs?

Upvotes: 2

Views: 9253

Answers (2)

akrun
akrun

Reputation: 887108

We can also do this in base R

aggregate(.~cyl, mtcars, FUN = function(x) sum(x==0))
#  cyl mpg disp hp drat wt qsec vs am gear carb
#1   4   0    0  0    0  0    0  1  3    0    0
#2   6   0    0  0    0  0    0  3  4    0    0
#3   8   0    0  0    0  0    0 14 12    0    0

Or with rowsum

rowsum(+(mtcars[-2]==0), group = mtcars$cyl)
#     mpg disp hp drat wt qsec vs am gear carb
#4   0    0  0    0  0    0  1  3    0    0
#6   0    0  0    0  0    0  3  4    0    0
#8   0    0  0    0  0    0 14 12    0    0

Or with data.table

library(data.table)
as.data.table(mtcars)[, lapply(.SD, function(x) sum(x==0)) , cyl]

Upvotes: 5

zacdav
zacdav

Reputation: 4671

because the . will be a vector in this case you can just use a logical test and sum because TRUE is treated as a 1 and FALSE is a 0.

mtcars %>% 
    group_by(cyl) %>% 
    summarise_each(funs(sum(.==0)))

    cyl   mpg  disp    hp  drat    wt  qsec    vs    am  gear  carb
  (dbl) (int) (int) (int) (int) (int) (int) (int) (int) (int) (int)
1     4     0     0     0     0     0     0     1     3     0     0
2     6     0     0     0     0     0     0     3     4     0     0
3     8     0     0     0     0     0     0    14    12     0     0

Upvotes: 7

Related Questions