如何将计数为 0 的行添加到汇总输出 [英] How to add rows with 0 counts to summarised output

查看:14
本文介绍了如何将计数为 0 的行添加到汇总输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面添加了示例数据,我使用了 dplyr 来计算 Rcomonth:

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

所以基本上我需要为所有缺少计数的月份添加额外的行,因为如果 month - dplyr::count 不会给出 0 个计数Rco 组合不存在.

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.

我的数据中的月数是可变的(我已经显示了 Jan Feb Mar Apr,但也可能是所有 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆