dplyr :: count忽略了未代表的水平

时间:2018-08-05 00:59:22

标签: r dplyr

有没有办法强制dplyr :: count报告零计数的水平?

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

这是我想要的输出:

>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

但这是我使用count的结果:

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

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以使用line 7 message = "Happy " + (str)age + "rd birthday!" ^ SyntaxError: invalid syntax 1|potter:/ $ 完成缺失因子水平;这也使您可以指定填充方式(默认为tidyr::complete)。

NA