2

I have this data frame:

> df <- data.frame(o = c(0,1,1,1), e = c(1, 1, 1, 1))
> df
  o e
1 0 1
2 1 1
3 1 1
4 1 1

And I want to get counts using certain rules. I tried this:

out <- df %>% 
    mutate(L = case_when(o == 1 & e == 1 ~ 'a',
                         o == 0 & e == 1 ~ 'b',
                         o == 1 & e == 0 ~ 'c',
                         o == 0 & e == 0 ~ 'd')) %>%
    group_by(L) %>% 
    summarise(n())

I saved a lot of time by doing that but I get this:

> out
# A tibble: 3 x 2
      l `n()`
  <chr> <int>
1     a     3
2     b     1

How can I include 0 values (c = 0 and d = 0) within the creation code of out in order to make it automatic for any letter (a, b, c or d)?

noriega
  • 447
  • 1
  • 9
  • 23
  • 1
    [Proper idiom for adding zero count rows in tidyr/dplyr](https://stackoverflow.com/questions/25956178/proper-idiom-for-adding-zero-count-rows-in-tidyr-dplyr) – Henrik Jan 05 '18 at 19:33

2 Answers2

1
data.frame(table(factor(x = paste(df$o, df$e),
                        levels = c("1 1", "0 1", "1 0", "0 0"),
                        labels = c("a", "b", "c", "d"))))
#  Var1 Freq
#1    a    3
#2    b    1
#3    c    0
#4    d    0
d.b
  • 32,245
  • 6
  • 36
  • 77
1

Since this is tagged with dplyr you could modify your code to be:

out <- df %>% 
  mutate(L = factor(case_when(o == 1 & e == 1 ~ 'a',
                              o == 0 & e == 1 ~ 'b',
                              o == 1 & e == 0 ~ 'c',
                              o == 0 & e == 0 ~ 'd'), 
                    levels = c('a', 'b', 'c', 'd'))) %>% 
  select(L) %>% table(L = .) %>% data.frame

As others pointed out, the key is to factor L and add all the necessary levels.

#out
#  L Freq
#1 a    3
#2 b    1
#3 c    0
#4 d    0
Mike H.
  • 13,960
  • 2
  • 29
  • 39