How to get cross tab (group_by) with dplyr to include 0s?

Viewed 81

I'm trying to replicate the output of a the table() function using dplyr. I've includes a copy of the data I'm working with at the very bottom.

When using table() I get a crosstab that includes 0 for missing crosstabulations.

As you can see the Outcome "Pending" is 0 for the month of "Feb"

> table(data$Month, data$Outcome)
     
      Negative Neutral Pending Positive
  Apr       48      12       1       75
  Feb       37       6       0       48
  Jan       49       9       0       44
  Jul       12       7     210       41
  Jun       66      47      35       89
  Mar       90      15       5      116
  May       51       9       4       40

I can save this into a workable df to make graphs using


> as.data.frame(table(data$Month, data$Outcome))
   Var1     Var2 Freq
1   Apr Negative   48
2   Feb Negative   37
3   Jan Negative   49
4   Jul Negative   12
5   Jun Negative   66
6   Mar Negative   90
7   May Negative   51
8   Apr  Neutral   12
9   Feb  Neutral    6
10  Jan  Neutral    9
11  Jul  Neutral    7
12  Jun  Neutral   47
13  Mar  Neutral   15
14  May  Neutral    9
15  Apr  Pending    1
16  Feb  Pending    0
17  Jan  Pending    0
18  Jul  Pending  210
19  Jun  Pending   35
20  Mar  Pending    5
21  May  Pending    4
22  Apr Positive   75
23  Feb Positive   48
24  Jan Positive   44
25  Jul Positive   41
26  Jun Positive   89
27  Mar Positive  116
28  May Positive   40

Now when I try to do this with dplyr, I can't get the 0 to appear.

> data %>% 
+   group_by(Month, Outcome) %>% tally() %>% data.frame() 
   Month  Outcome   n
1    Apr Negative  48
2    Apr  Neutral  12
3    Apr  Pending   1
4    Apr Positive  75
5    Feb Negative  37
6    Feb  Neutral   6
7    Feb Positive  48
8    Jan Negative  49
9    Jan  Neutral   9
10   Jan Positive  44
11   Jul Negative  12
12   Jul  Neutral   7
13   Jul  Pending 210
14   Jul Positive  41
15   Jun Negative  66
16   Jun  Neutral  47
17   Jun  Pending  35
18   Jun Positive  89
19   Mar Negative  90
20   Mar  Neutral  15
21   Mar  Pending   5
22   Mar Positive 116
23   May Negative  51
24   May  Neutral   9
25   May  Pending   4
26   May Positive  40

My goal is to get the same output as using as.data.frame(table(data$Month, data$Outcome)) using dplyr

Thank you.

DATA

> dput(data)
structure(list(Month = c("Jun", "Mar", "Jan", "Mar", "Mar", "Apr", 
"Jul", "Jun", "Jun", "Jul", "Jan", "Mar", "Jan", "Jan", "Feb", 
"Jan", "Jan", "Apr", "Mar", "Mar", "Mar", "Mar", "Apr", "Mar", 
"Mar", "Mar", "Mar", "Mar", "Mar", "Apr", "Mar", "Mar", "Jul", 
"Apr", "May", "Mar", "Apr", "Jun", "Apr", "Apr", "Apr", "Jun", 
"May", "May", "Jun", "Jun", "Jul", "Jul", "Jul", "Jun", "May", 
"Jun", "Jun", "May", "Jun", "May", "May", "May", "May", "Jul", 
"Jun", "Mar", "Mar", "Mar", "Apr", "May", "Jun", "Mar", "May", 
"May", "Jul", "Jul", "Mar", "Mar", "Mar", "Jul", "Mar", "Mar", 
"Mar", "Mar", "Mar", "Jul", "Jul", "Jul", "Mar", "Jul", "Mar", 
"Jul", "Jul", "Jun", "May", "May", "Jul", "Jan", "Mar", "Mar", 
"Jul", "Mar", "Jun", "Jan", "Jul", "Jan", "Jul", "Mar", "Apr", 
"Apr", "Apr", "Mar", "Mar", "Mar", "Apr", "Apr", "Jun", "Jun", 
"Jun", "May", "May", "Apr", "Jun", "Jun", "Jun", "May", "Jul", 
"Jun", "Jun", "Jun", "May", "Jun", "Jul", "Jul", "Jul", "Jul", 
"Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jan", "Mar", "Jan", 
"Jan", "Mar", "Feb", "Apr", "Feb", "Feb", "Feb", "Mar", "Jan", 
"Jan", "Mar", "Jan", "Jan", "Jan", "May", "Jun", "Apr", "Mar", 
"Mar", "May", "Mar", "Mar", "Mar", "Feb", "Feb", "Mar", "May", 
"Apr", "Apr", "Jul", "Jul", "Jul", "Jul", "Apr", "Apr", "Apr", 
"Apr", "Apr", "Apr", "Jun", "Jun", "Jul", "Jul", "Jul", "May", 
"May", "Jun", "Apr", "May", "May", "Apr", "Apr", "Apr", "May", 
"May", "May", "May", "Mar", "Mar", "Mar", "Jul", "Mar", "Mar", 
"Jul", "May", "Jul", "Mar", "Mar", "Apr", "Mar", "Jul", "Mar", 
"Jan", "Mar", "May", "Jul", "Jul", "Jul", "Mar", "Jul", "Jan", 
"Jan", "Jan", "Mar", "Mar", "Mar", "Jul", "Apr", "Apr", "Mar", 
"Jan", "Jan", "Jan", "Jan", "Jan", "Mar", "Jul", "Jun", "Jun", 
"Jun", "Jun", "Jul", "Jun", "Apr", "May", "Apr", "Apr", "Mar", 
"Jul", "Mar", "May", "May", "May", "May", "Apr", "Jul", "May", 
"Jul", "Jul", "Jun", "Jul", "May", "May", "Jul", "May", "May", 
"Jun", "May", "Jun", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", 
"Feb", "Feb", "May", "Feb", "Apr", "Feb", "Jun", "Feb", "Mar", 
"Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Jun", 
"Feb", "Mar", "Feb", "Mar", "Mar", "Feb", "Feb", "Apr", "Feb", 
"Jul", "Jul", "Jul", "Jul", "Jun", "Jun", "Jun", "Jun", "Jun", 
"Jun", "May", "Jul", "Jun", "May", "May", "Apr", "Apr", "Jul", 
"Mar", "May", "May", "Jul", "Jul", "Apr", "May", "Feb", "May", 
"Jul", "Jul", "Jul", "Jul", "Jun", "Jul", "Feb", "Feb", "Feb", 
"Mar", "Feb", "Feb", "Feb", "Mar", "Mar", "Feb", "Feb", "Feb", 
"Mar", "Jan", "Jan", "Feb", "Jan", "Mar", "Mar", "Mar", "Feb", 
"Mar", "Mar", "Mar", "Mar", "Mar", "Feb", "Mar", "Mar", "Jan", 
"Feb", "Feb", "Feb", "Jan", "Mar", "Jan", "Feb", "Mar", "Jan", 
"Mar", "Mar", "Feb", "Mar", "Feb", "Jan", "Feb", "Jan", "Feb", 
"Feb", "Feb", "Mar", "Feb", "Feb", "Apr", "Jun", "May", "Mar", 
"Mar", "Mar", "Jul", "Jun", "Jul", "May", "Jun", "May", "May", 
"Jun", "Jun", "May", "May", "Jun", "May", "Jun", "Jul", "Jul", 
"Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jun", "Jun", 
"Jun", "Jun", "Jun", "Jul", "Jun", "May", "Jun", "Jun", "Mar", 
"Jan", "Jan", "Apr", "Jan", "Mar", "Mar", "Mar", "May", "Mar", 
"Mar", "Mar", "Mar", "Mar", "Mar", "Feb", "Mar", "Feb", "Mar", 
"Feb", "Jan", "Jan", "Jan", "Feb", "Feb", "Jan", "Apr", "Mar", 
"Mar", "Jul", "Mar", "Mar", "Jul", "Apr", "Jul", "Jul", "Jul", 
"Apr", "Jun", "Mar", "Mar", "Jul", "Mar", "Apr", "Apr", "Apr", 
"Mar", "Apr", "Apr", "Jan", "Jan", "Mar", "Jun", "Mar", "Apr", 
"Jun", "Apr", "Jun", "Apr", "Apr", "Jun", "Jun", "Jun", "Jun", 
"Jun", "Jun", "Jul", "Jul", "Jul", "Jun", "Jun", "Jul", "Jun", 
"Jul", "Apr", "Apr", "Apr", "Jul", "Jun", "Jun", "Jul", "Jul", 
"Jun", "Jun", "Mar", "Jul", "Jan", "Jan", "Jan", "Mar", "Jan", 
"Mar", "Mar", "Jan", "Mar", "Jan", "Jun", "Jan", "Jan", "Jan", 
"Mar", "Jun", "Jul", "Jun", "Jun", "May", "May", "Jul", "Apr", 
"May", "May", "May", "May", "May", "May", "Apr", "Apr", "Apr", 
"Apr", "Apr", "Apr", "Apr", "Jan", "Jan", "Feb", "Feb", "Feb", 
"Feb", "Apr", "Apr", "Apr", "Apr", "Jul", "Jul", "Jul", "Jul", 
"Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", 
"Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", 
"Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", 
"Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", 
"Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", 
"Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", 
"Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", 
"Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", 
"Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", 
"Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", 
"Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", 
"Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", 
"Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", 
"Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", 
"Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", 
"Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Jul", "Mar", 
"Mar", "Mar", "Jul", "Apr", "Feb", "Feb", "Apr", "Feb", "Feb", 
"Feb", "May", "May", "May", "Jun", "Jun", "Jun", "Jul", "Jul", 
"Jun", "Jun", "Jun", "Jun", "May", "May", "Apr", "Apr", "May", 
"Apr", "Jul", "Apr", "Apr", "May", "Apr", "May", "May", "Mar", 
"Mar", "Apr", "Feb", "Apr", "Jun", "Jun", "Feb", "Feb", "Jul", 
"Apr", "Jun", "Apr", "Jul", "Apr", "May", "Jul", "Mar", "Mar", 
"Mar", "Mar", "Jan", "Jan", "Mar", "Jan", "Feb", "Feb", "Feb", 
"Mar", "Feb", "Apr", "Mar", "Jan", "Apr", "Feb", "Apr", "Feb", 
"Jun", "Mar", "Feb", "Feb", "Feb", "Mar", "Jun", "Mar", "Jul", 
"Feb", "Mar", "Mar", "Apr", "Mar", "Apr", "Mar", "Mar", "Mar", 
"Mar", "Apr", "Mar", "Mar", "Mar", "Mar", "Mar", "May", "Mar", 
"Mar", "Mar", "Mar", "Apr", "Mar", "Mar", "Feb", "Mar", "Jan", 
"Jan", "Jan", "Mar", "Feb", "Mar", "Feb", "Jul", "Jul", "Jun", 
"Jun", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun", 
"Jul", "Jun", "Jun", "Jun", "Jun", "Jul", "Jul", "Jul", "Jul", 
"May", "May", "May", "Apr", "Jun", "Apr", "Jun", "Apr", "Apr", 
"Apr", "Apr", "Mar", "May", "Mar", "Mar", "Apr", "May", "Apr", 
"Apr", "Apr", "Jun", "May", "May", "May", "Apr", "Apr", "Apr", 
"Jun", "May", "Jun", "Jun", "Jun", "May", "Feb", "Feb", "Mar", 
"Jan", "Feb", "Apr", "Jan", "Jan", "Jan", "Feb", "Feb", "Jul", 
"Jul", "Jun", "Jul", "Jul", "Jul", "Apr", "Apr", "Apr", "Apr", 
"Mar", "May", "Apr", "Mar", "Jan", "Mar", "Apr", "Mar", "Apr", 
"Apr", "Jan", "Apr", "Jan", "Jan", "Apr", "Jun", "Jun", "Jun", 
"Jun", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun", "Apr", "Apr", 
"Apr", "Apr", "Jun", "Apr", "Jun", "Jun", "Apr", "Jun", "Apr", 
"Jun", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun", 
"Jun", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun", 
"Jun", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun", 
"Jun", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun", 
"Jun", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun", 
"Jun", "Jun", "Jun", "Jun", "Jun", "Mar", "Jun", "Jun", "Mar", 
"Jun", "Jun", "Jan", "Jan", "Jun", "Jun", "Jun", "Jun", "Jun", 
"Apr", "Mar", "Mar", "Mar", "Jun", "Jan", "Apr", "Jun", "Jun", 
"Jan", "Mar", "Mar", "Jun", "Jun", "Mar", "Jun", "Jun", "Jun", 
"Jun", "Jun", "Apr", "Mar", "Mar", "Apr", "Mar", "Jun", "Jan", 
"Jun", "Jun", "Mar", "Mar", "Jun", "Jun", "Jun", "Jun", "Jun", 
"Apr", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun", 
"Jun", "Jun", "Jun", "Mar", "Jun", "Jun", "Jan", "May", "May", 
"Jun", "Jan", "May", "Jan", "Feb", "Mar", "Jan", "Mar", "Mar", 
"Mar", "Mar", "Mar", "Jan", "Jan", "Feb", "Mar", "Jan", "Jan", 
"Jan", "Feb", "Mar", "Feb", "Jun", "Mar", "Mar", "Mar", "Jan", 
"Feb", "Feb", "Jan", "Feb", "Feb", "Jan", "Mar", "Feb", "Feb", 
"Jan", "Mar", "Mar", "Jan", "Feb", "Jun", "Jun", "Jun", "Jun", 
"Jun", "Mar", "Mar", "Mar", "May", "May", "May", "May", "Mar", 
"Mar", "Mar", "May", "Jan", "Jan", "May", "Feb", "Mar", "Mar", 
"Mar", "Jan", "Jan", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", 
"Mar", "Jan", "Jan", "May", "May", "May", "May", "May", "Jan", 
"Jan", "Apr", "May", "Apr", "Mar", "Feb", "Jan", "Mar", "Jan", 
"Jan", "Feb", "Jan", "Mar", "Apr", "Jun", "Mar", "Mar", "Apr", 
"Mar", "Jun", "Mar", "Mar", "Jan", "Apr", "Apr", "Apr", "Apr", 
"Apr", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun", "Jun"), Outcome = c("Negative", 
"Positive", "Negative", "Negative", "Positive", "Positive", "Negative", 
"Negative", "Negative", "Pending", "Negative", "Negative", "Positive", 
"Negative", "Negative", "Negative", "Negative", "Negative", "Negative", 
"Negative", "Positive", "Negative", "Positive", "Positive", "Negative", 
"Positive", "Negative", "Negative", "Positive", "Positive", "Positive", 
"Negative", "Negative", "Neutral", "Negative", "Negative", "Negative", 
"Neutral", "Negative", "Neutral", "Negative", "Positive", "Negative", 
"Positive", "Neutral", "Positive", "Positive", "Positive", "Pending", 
"Positive", "Negative", "Positive", "Positive", "Negative", "Positive", 
"Negative", "Negative", "Positive", "Positive", "Pending", "Positive", 
"Positive", "Positive", "Positive", "Positive", "Positive", "Negative", 
"Positive", "Positive", "Negative", "Positive", "Pending", "Positive", 
"Positive", "Negative", "Positive", "Positive", "Negative", "Negative", 
"Positive", "Negative", "Pending", "Positive", "Pending", "Negative", 
"Pending", "Positive", "Pending", "Pending", "Positive", "Negative", 
"Negative", "Negative", "Negative", "Positive", "Negative", "Positive", 
"Positive", "Positive", "Negative", "Pending", "Negative", "Pending", 
"Negative", "Negative", "Negative", "Negative", "Negative", "Negative", 
"Positive", "Negative", "Negative", "Positive", "Negative", "Negative", 
"Neutral", "Negative", "Positive", "Negative", "Negative", "Positive", 
"Negative", "Pending", "Neutral", "Positive", "Neutral", "Positive", 
"Pending", "Positive", "Pending", "Pending", "Positive", "Pending", 
"Neutral", "Pending", "Neutral", "Pending", "Pending", "Neutral", 
"Negative", "Negative", "Negative", "Negative", "Negative", "Negative", 
"Positive", "Positive", "Positive", "Negative", "Negative", "Neutral", 
"Positive", "Negative", "Positive", "Negative", "Positive", "Negative", 
"Negative", "Positive", "Negative", "Positive", "Negative", "Negative", 
"Positive", "Neutral", "Negative", "Negative", "Negative", "Neutral", 
"Negative", "Pending", "Pending", "Pending", "Positive", "Positive", 
"Positive", "Neutral", "Positive", "Negative", "Positive", "Positive", 
"Negative", "Pending", "Pending", "Pending", "Positive", "Positive", 
"Neutral", "Positive", "Positive", "Positive", "Positive", "Positive", 
"Positive", "Positive", "Positive", "Positive", "Positive", "Positive", 
"Positive", "Negative", "Positive", "Negative", "Positive", "Positive", 
"Negative", "Positive", "Positive", "Negative", "Negative", "Negative", 
"Positive", "Positive", "Positive", "Negative", "Positive", "Positive", 
"Positive", "Positive", "Negative", "Positive", "Positive", "Positive", 
"Positive", "Negative", "Negative", "Negative", "Positive", "Negative", 
"Positive", "Positive", "Positive", "Negative", "Positive", "Negative", 
"Positive", "Positive", "Positive", "Positive", "Positive", "Negative", 
"Positive", "Positive", "Positive", "Positive", "Neutral", "Positive", 
"Positive", "Positive", "Positive", "Negative", "Positive", "Positive", 
"Negative", "Negative", "Positive", "Positive", "Negative", "Positive", 
"Pending", "Negative", "Positive", "Negative", "Negative", "Positive", 
"Positive", "Neutral", "Neutral", "Positive", "Negative", "Positive", 
"Positive", "Negative", "Positive", "Negative", "Positive", "Negative", 
"Negative", "Positive", "Negative", "Positive", "Negative", "Neutral", 
"Positive", "Negative", "Negative", "Negative", "Negative", "Negative", 
"Negative", "Negative", "Negative", "Positive", "Negative", "Negative", 
"Positive", "Positive", "Positive", "Negative", "Negative", "Negative", 
"Neutral", "Negative", "Positive", "Pending", "Negative", "Positive", 
"Neutral", "Pending", "Negative", "Neutral", "Negative", "Negative", 
"Positive", "Pending", "Neutral", "Neutral", "Negative", "Negative", 
"Neutral", "Pending", "Positive", "Negative", "Positive", "Pending", 
"Pending", "Negative", "Negative", "Neutral", "Negative", "Pending", 
"Pending", "Pending", "Pending", "Pending", "Pending", "Positive", 
"Positive", "Positive", "Positive", "Positive", "Positive", "Positive", 
"Positive", "Positive", "Positive", "Positive", "Positive", "Positive", 
"Positive", "Positive", "Positive", "Positive", "Positive", "Positive", 
"Positive", "Positive", "Positive", "Positive", "Positive", "Positive", 
"Positive", "Positive", "Positive", "Positive", "Positive", "Positive", 
"Positive", "Positive", "Positive", "Positive", "Positive", "Positive", 
"Positive", "Positive", "Positive", "Positive", "Positive", "Positive", 
"Positive", "Positive", "Positive", "Positive", "Positive", "Positive", 
"Positive", "Positive", "Positive", "Positive", "Positive", "Negative", 
"Negative", "Positive", "Positive", "Neutral", "Negative", "Neutral", 
"Negative", "Negative", "Negative", "Negative", "Negative", "Negative", 
"Negative", "Negative", "Negative", "Positive", "Negative", "Negative", 
"Pending", "Pending", "Pending", "Pending", "Pending", "Pending", 
"Pending", "Pending", "Pending", "Negative", "Positive", "Negative", 
"Positive", "Positive", "Pending", "Positive", "Positive", "Positive", 
"Positive", "Negative", "Neutral", "Negative", "Positive", "Positive", 
"Neutral", "Positive", "Negative", "Positive", "Positive", "Negative", 
"Positive", "Neutral", "Positive", "Positive", "Positive", "Positive", 
"Positive", "Negative", "Negative", "Negative", "Neutral", "Negative", 
"Positive", "Negative", "Negative", "Positive", "Positive", "Positive", 
"Pending", "Positive", "Negative", "Pending", "Positive", "Pending", 
"Pending", "Pending", "Positive", "Positive", "Positive", "Neutral", 
"Pending", "Negative", "Negative", "Positive", "Positive", "Negative", 
"Positive", "Negative", "Negative", "Neutral", "Positive", "Positive", 
"Negative", "Positive", "Positive", "Positive", "Positive", "Negative", 
"Positive", "Negative", "Positive", "Pending", "Positive", "Negative", 
"Negative", "Positive", "Neutral", "Positive", "Positive", "Neutral", 
"Pending", "Pending", "Pending", "Negative", "Positive", "Positive", 
"Pending", "Negative", "Negative", "Pending", "Pending", "Positive", 
"Positive", "Pending", "Pending", "Positive", "Positive", "Negative", 
"Negative", "Positive", "Positive", "Positive", "Positive", "Positive", 
"Negative", "Positive", "Negative", "Negative", "Negative", "Pending", 
"Neutral", "Neutral", "Positive", "Positive", "Positive", "Negative", 
"Neutral", "Positive", "Neutral", "Negative", "Negative", "Neutral", 
"Neutral", "Negative", "Positive", "Neutral", "Neutral", "Neutral", 
"Neutral", "Positive", "Neutral", "Positive", "Positive", "Positive", 
"Negative", "Positive", "Positive", "Negative", "Positive", "Positive", 
"Positive", "Pending", "Pending", "Neutral", "Pending", "Pending", 
"Pending", "Pending", "Pending", "Pending", "Pending", "Pending", 
"Pending", "Pending", "Pending", "Pending", "Pending", "Pending", 
"Pending", "Pending", "Pending", "Pending", "Pending", "Pending", 
"Pending", "Pending", "Pending", "Pending", "Pending", "Pending", 
"Pending", "Pending", "Pending", "Pending", "Pending", "Pending", 
"Pending", "Neutral", "Pending", "Pending", "Pending", "Pending", 
"Pending", "Pending", "Pending", "Pending", "Pending", "Pending", 
"Pending", "Pending", "Pending", "Pending", "Pending", "Pending", 
"Pending", "Pending", "Pending", "Pending", "Pending", "Pending", 
"Pending", "Pending", "Pending", "Pending", "Pending", "Pending", 
"Pending", "Pending", "Pending", "Pending", "Pending", "Pending", 
"Pending", "Pending", "Pending", "Pending", "Pending", "Pending", 
"Pending", "Pending", "Pending", "Pending", "Pending", "Pending", 
"Pending", "Pending", "Pending", "Pending", "Pending", "Pending", 
"Pending", "Pending", "Pending", "Pending", "Pending", "Pending", 
"Pending", "Pending", "Pending", "Pending", "Pending", "Pending", 
"Pending", "Pending", "Pending", "Pending", "Pending", "Pending", 
"Pending", "Pending", "Pending", "Pending", "Pending", "Pending", 
"Pending", "Pending", "Pending", "Pending", "Pending", "Pending", 
"Pending", "Pending", "Pending", "Pending", "Pending", "Pending", 
"Pending", "Pending", "Pending", "Pending", "Pending", "Pending", 
"Pending", "Pending", "Pending", "Pending", "Pending", "Pending", 
"Pending", "Pending", "Pending", "Pending", "Pending", "Pending", 
"Pending", "Pending", "Pending", "Pending", "Positive", "Negative", 
"Neutral", "Positive", "Negative", "Positive", "Negative", "Neutral", 
"Negative", "Positive", "Negative", "Positive", "Negative", "Positive", 
"Neutral", "Positive", "Neutral", "Positive", "Pending", "Positive", 
"Positive", "Pending", "Negative", "Negative", "Negative", "Positive", 
"Positive", "Negative", "Positive", "Pending", "Negative", "Positive", 
"Negative", "Negative", "Positive", "Positive", "Negative", "Positive", 
"Positive", "Negative", "Negative", "Negative", "Positive", "Negative", 
"Positive", "Pending", "Negative", "Positive", "Positive", "Pending", 
"Negative", "Negative", "Negative", "Positive", "Negative", "Negative", 
"Positive", "Negative", "Positive", "Positive", "Positive", "Positive", 
"Positive", "Negative", "Neutral", "Negative", "Positive", "Positive", 
"Positive", "Negative", "Negative", "Positive", "Negative", "Positive", 
"Negative", "Negative", "Negative", "Negative", "Positive", "Positive", 
"Negative", "Pending", "Positive", "Positive", "Negative", "Negative", 
"Positive", "Positive", "Positive", "Negative", "Negative", "Positive", 
"Negative", "Negative", "Positive", "Positive", "Positive", "Negative", 
"Negative", "Positive", "Positive", "Positive", "Positive", "Positive", 
"Negative", "Positive", "Negative", "Negative", "Positive", "Positive", 
"Negative", "Negative", "Negative", "Negative", "Negative", "Positive", 
"Positive", "Pending", "Positive", "Pending", "Positive", "Pending", 
"Negative", "Positive", "Positive", "Negative", "Positive", "Negative", 
"Positive", "Negative", "Positive", "Positive", "Positive", "Positive", 
"Positive", "Pending", "Negative", "Negative", "Neutral", "Negative", 
"Neutral", "Negative", "Negative", "Positive", "Negative", "Positive", 
"Negative", "Positive", "Negative", "Negative", "Neutral", "Negative", 
"Negative", "Negative", "Negative", "Positive", "Negative", "Positive", 
"Negative", "Negative", "Positive", "Positive", "Positive", "Neutral", 
"Negative", "Positive", "Negative", "Negative", "Negative", "Negative", 
"Positive", "Positive", "Negative", "Positive", "Positive", "Negative", 
"Negative", "Positive", "Negative", "Positive", "Pending", "Negative", 
"Positive", "Pending", "Negative", "Positive", "Positive", "Positive", 
"Positive", "Negative", "Negative", "Pending", "Positive", "Neutral", 
"Positive", "Negative", "Positive", "Negative", "Negative", "Pending", 
"Negative", "Negative", "Negative", "Negative", "Positive", "Neutral", 
"Neutral", "Negative", "Pending", "Neutral", "Neutral", "Positive", 
"Positive", "Pending", "Pending", "Positive", "Positive", "Positive", 
"Positive", "Neutral", "Positive", "Neutral", "Neutral", "Positive", 
"Positive", "Negative", "Negative", "Positive", "Neutral", "Negative", 
"Negative", "Neutral", "Negative", "Neutral", "Negative", "Negative", 
"Neutral", "Positive", "Positive", "Neutral", "Pending", "Positive", 
"Positive", "Neutral", "Pending", "Positive", "Negative", "Pending", 
"Neutral", "Negative", "Negative", "Positive", "Negative", "Pending", 
"Positive", "Positive", "Pending", "Positive", "Negative", "Pending", 
"Pending", "Pending", "Negative", "Pending", "Pending", "Neutral", 
"Pending", "Pending", "Positive", "Negative", "Neutral", "Pending", 
"Pending", "Pending", "Positive", "Pending", "Negative", "Pending", 
"Pending", "Negative", "Positive", "Positive", "Positive", "Negative", 
"Positive", "Positive", "Neutral", "Pending", "Pending", "Positive", 
"Positive", "Positive", "Positive", "Neutral", "Positive", "Positive", 
"Neutral", "Neutral", "Positive", "Positive", "Positive", "Neutral", 
"Neutral", "Positive", "Positive", "Pending", "Negative", "Neutral", 
"Pending", "Positive", "Neutral", "Positive", "Positive", "Negative", 
"Negative", "Positive", "Negative", "Negative", "Positive", "Positive", 
"Positive", "Positive", "Positive", "Positive", "Neutral", "Negative", 
"Positive", "Neutral", "Neutral", "Positive", "Positive", "Negative", 
"Negative", "Positive", "Neutral", "Neutral", "Neutral", "Positive", 
"Negative", "Negative", "Negative", "Pending", "Neutral", "Pending", 
"Positive", "Pending", "Positive", "Positive", "Positive", "Negative", 
"Positive", "Positive", "Neutral", "Pending", "Positive", "Negative", 
"Neutral", "Negative", "Pending", "Negative", "Negative", "Negative", 
"Negative", "Neutral", "Neutral", "Negative", "Negative", "Positive", 
"Neutral", "Negative", "Neutral", "Negative", "Positive", "Negative", 
"Neutral", "Positive", "Negative", "Negative", "Neutral", "Negative", 
"Negative", "Neutral", "Positive", "Negative", "Positive", "Positive", 
"Neutral", "Positive", "Positive", "Negative", "Negative", "Neutral", 
"Negative", "Positive", "Negative", "Pending", "Positive", "Positive", 
"Positive", "Negative", "Negative", "Positive", "Positive", "Positive", 
"Positive", "Negative", "Pending", "Negative", "Negative", "Negative", 
"Negative", "Positive", "Positive", "Negative", "Negative", "Negative", 
"Positive", "Neutral", "Positive", "Positive", "Positive", "Positive", 
"Positive", "Positive", "Negative", "Negative", "Positive", "Positive", 
"Neutral", "Positive", "Neutral", "Positive", "Negative", "Negative", 
"Positive", "Negative", "Positive", "Negative", "Negative", "Negative", 
"Negative", "Positive", "Positive", "Negative", "Positive", "Negative", 
"Neutral", "Positive", "Positive", "Negative", "Negative", "Negative", 
"Positive", "Positive", "Positive", "Negative", "Positive", "Negative", 
"Negative")), row.names = c(NA, -1166L), class = c("tbl_df", 
"tbl", "data.frame"))




3 Answers

I'd suggest using tidyr::complete, which would be loaded in along with dplyr if you loaded library(tidyverse).

data %>% 
  count(Month, Outcome) %>% # shortcut for group_by %>% tally
  tidyr::complete(Month, Outcome, fill = list(n = 0))

Result

# A tibble: 28 x 3
   Month Outcome      n
   <chr> <chr>    <dbl>
 1 Apr   Negative    48
 2 Apr   Neutral     12
 3 Apr   Pending      1
 4 Apr   Positive    75
 5 Feb   Negative    37
 6 Feb   Neutral      6
 7 Feb   Pending      0  # <-- added with complete() 
 8 Feb   Positive    48
 9 Jan   Negative    49
10 Jan   Neutral      9
# … with 18 more rows

An easier option is to use tabyl which returns similar output as table and then use pivot_longer from tidyr to reshape the data from 'wide' to 'long'

library(janitor)
library(tidyr)
library(dplyr)
tabyl(data, Month, Outcome) %>%
    pivot_longer(cols = -Month, names_to = "Outcome", values_to = "n")

-output

# A tibble: 28 x 3
   Month Outcome      n
   <chr> <chr>    <dbl>
 1 Apr   Negative    48
 2 Apr   Neutral     12
 3 Apr   Pending      1
 4 Apr   Positive    75
 5 Feb   Negative    37
 6 Feb   Neutral      6
 7 Feb   Pending      0
 8 Feb   Positive    48
 9 Jan   Negative    49
10 Jan   Neutral      9
# … with 18 more rows

Here is one more option using expand:

library(tidyverse)
count_data <- data %>% 
    count(Month, Outcome)  
    
count_data %>% 
    expand(Month, Outcome) %>% 
    left_join(count_data) %>% 
    replace(is.na(.), 0)
   Month Outcome      n
   <chr> <chr>    <int>
 1 Apr   Negative    48
 2 Apr   Neutral     12
 3 Apr   Pending      1
 4 Apr   Positive    75
 5 Feb   Negative    37
 6 Feb   Neutral      6
 7 Feb   Pending      0
 8 Feb   Positive    48
 9 Jan   Negative    49
10 Jan   Neutral      9
# ... with 18 more rows
Related