如何将0计数的行添加到汇总输出 [英] How to add rows with 0 counts to summarised output
问题描述
我在下面添加了示例数据,我已经使用dplyr指望 Rco
和 month
:
I have added sample data below, I have used dplyr to count on Rco
and month
:
structure(list(Rco = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 3L, 3L, 4L, 4L, 4L), .Label = c("A220", "B334", "C123", "D445"
), class = "factor"), month = structure(c(3L, 2L, 4L, 1L, 3L,
2L, 4L, 1L, 3L, 4L, 2L, 4L, 3L), .Label = c("Apr", "Feb", "Jan",
"Mar"), class = "factor"), count = c(1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13)), .Names = c("Rco", "month", "count"), row.names = c(NA,
-13L), class = "data.frame")
是否可以将数据转换为:
Is there a way to transform this data to:
structure(list(Rco = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L), .Label = c("A220", "B334",
"C123", "D445"), class = "factor"), month = structure(c(3L, 2L,
4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L), .Label = c("Apr",
"Feb", "Jan", "Mar"), class = "factor"), count = c(1, 2, 3, 4,
5, 6, 7, 8, 9, 0, 10, 0, 13, 11, 12, 0)), .Names = c("Rco", "month",
"count"), row.names = c(NA, -16L), class = "data.frame")
所以基本上,我需要为所有缺少计数的月份添加额外的行,因为 dplyr :: count 如果
月
- Rco
组合不存在,则code>不给出0计数。
So basically I need to add extra rows for all months that have missing count, because dplyr::count
does not give 0 counts if a month
- Rco
combination does not exist.
我的数据中的月份数是可变的(我显示了1月2月3月4月,但也可能是全部12个月),所以请有人为我提供动态解决方案,我将不胜感激。
The number of months is variable in my data ( I have shown Jan Feb Mar Apr but it could be for all 12 months as well) , so please if someone can provide me a dynamic solution, I would be grateful.
推荐答案
您可以使用 tidyr :: complete
,并将填充指定为0(而不是默认值NA):
You can use tidyr::complete
and specify the fill to be 0 (instead of the default NA):
library(tidyr)
complete(df, Rco, month, fill = list(count = 0))
这篇关于如何将0计数的行添加到汇总输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!