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 use count for multiple column counts in one run

Usually, when I count a column in a data frame, I use the count() function from the dplyr package.

library(dplyr)

mtcars %>% 
  count(cyl) 

and

mtcars %>% 
  count(am) 

and

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

mtcars %>% 
  count(gear) 

etc…
Now I would like to count cyl, am, gear in one run.

Note: I don’t mean

mtcars %>%
 count(cyl, am, gear)

My working approach so far:


library(dplyr)
library(tidyr)

mtcars %>% 
  count(am) %>% 
bind_rows(mtcars %>% 
            count(cyl)) %>% 
  bind_rows(mtcars %>% 
              count(gear)) %>% 
pivot_longer(-n, 
             values_drop_na = TRUE) %>% 
  unite("variable", c("name", "value")) %>% 
  relocate(variable, n)
  variable     n
  <chr>    <int>
1 am_0        19
2 am_1        13
3 cyl_4       11
4 cyl_6        7
5 cyl_8       14
6 gear_3      15
7 gear_4      12
8 gear_5       5

I’m wondering if there’s a more concise way to achieve this.

>Solution :

Perhaps pivot and count?

library(dplyr)
library(tidyr) # pivot_longer
select(mtcars, am, cyl, gear) |>
  pivot_longer(cols = everything()) |>
  count(name, value)
# # A tibble: 8 × 3
#   name  value     n
#   <chr> <dbl> <int>
# 1 am        0    19
# 2 am        1    13
# 3 cyl       4    11
# 4 cyl       6     7
# 5 cyl       8    14
# 6 gear      3    15
# 7 gear      4    12
# 8 gear      5     5

You can "clean up" the names if you want by pasting them together.

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