Sam Lipworth
Sam Lipworth

Reputation: 107

Show groups with 0 group_by dplyr

I have a dataframe eg.

a<-data.frame(
   year=c(2001,2001,2002,2004,2001,2002,2004),
   binary=c(1,0,1,0,1,0,0))

I try for example:

a %>% group_by(year) %>% count()

I would like the output:

year    binary   n
2001    0        1
2001    1        1
2002    1        1
2002    0        1
2004    0        1
2004    1        0

I want to plot how many samples have the 1 phenotype but group_by deos not show a 1 group for 2004 because it has count 0. How can I make it do so?

Upvotes: 0

Views: 367

Answers (1)

Thomas K
Thomas K

Reputation: 3311

I think you are looking for tidyr::complete():

library(dplyr)
library(tidyr)

a<-data.frame(
  year=c(2001,2001,2002,2004,2001,2002,2004),
  binary=c(1,0,1,0,1,0,0))

a %>% 
  group_by(year) %>% 
  count(binary) %>% 
  ungroup() %>% 
  complete(year, binary, fill = list(n = 0))
#> # A tibble: 6 x 3
#>    year binary     n
#>   <dbl>  <dbl> <dbl>
#> 1  2001      0     1
#> 2  2001      1     2
#> 3  2002      0     1
#> 4  2002      1     1
#> 5  2004      0     2
#> 6  2004      1     0

Created on 2018-10-19 by the reprex package (v0.2.1)

Upvotes: 4

Related Questions