1

Is there a way to force dplyr::count to reports levels with zero counts?

>library(dplyr)
>df <- dplyr::data_frame(id = c(1,2,3,4,5,6), condition = c("A", "B","C", "A", "A", "B"))
>df$condition <- factor(df$condition, levels = c("A", "B", "C", "D", "E", "F", "G"))

This is my desired output:

>table(df$condition) %>% as_data_frame() %>% dplyr::rename(condition = Var1)
# A tibble: 7 x 2
  condition     n
  <chr>     <int>
1 A         3
2 B         2
3 C         1
4 D         0
5 E         0
6 F         0
7 G         0

But this is what I get using count:

>df %>% dplyr::count(condition)
# A tibble: 3 x 2
  condition     n
  <fct>     <int>
1 A         3
2 B         2
3 C         1

Thank you !

Yvan
  • 101
  • 2
  • 9
  • 5
    You can get the expected results using `fct_count()` in the forcats package. In your case, you type `fct_count(df$condition)`. – jazzurro Aug 05 '18 at 01:13

1 Answers1

1

You can use tidyr::complete to complete the missing factor levels; this also gives you the option to specify how to fill (default is NA).

library(dplyr)
library(tidyr)
df %>% count(condition) %>% complete(condition, fill = list(n = 0))
## A tibble: 7 x 2
#  condition     n
#  <fct>     <dbl>
#1 A            3.
#2 B            2.
#3 C            1.
#4 D            0.
#5 E            0.
#6 F            0.
#7 G            0.
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68