如何使用 dplyr 获取交叉表(group_by)以包含 0?

RL_*_*Pug 1 r dplyr tidyr

我正在尝试使用 dplyr 复制 table() 函数的输出。我在最底部包含了我正在使用的数据的副本。

使用 table() 时,我得到一个交叉表,其中包含 0 表示缺失的交叉表。

如您所见,“二月”的结果“待定”为 0

> 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

Run Code Online (Sandbox Code Playgroud)

我可以将它保存到一个可行的 df 中以使用


> 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
Run Code Online (Sandbox Code Playgroud)

现在,当我尝试使用 dplyr 执行此操作时,无法显示 0。

> 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
Run Code Online (Sandbox Code Playgroud)

我的目标是使用 dplyr 获得与使用 as.data.frame(table(data$Month, data$Outcome)) 相同的输出

谢谢你。

数据

> 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"

Jon*_*ing 5

我建议使用tidyr::completedplyr如果您加载library(tidyverse).

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

结果

# 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
Run Code Online (Sandbox Code Playgroud)