r 中的聚合正在删除 table 中的 0 个计数。如何让它显示?

aggregate in r is removing 0 count in table. How to make it show?

我正在尝试使用 aggregate 生成 table,以便使用 aggregate 中的 FUN 参数进行进一步处理。这是我的代码

var <- c(1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0)

aggregate(var, list(group), FUN = table)

但是,结果显示如下,其中第 4 组没有第二级(0 或 1)的值

  Group.1      x
1       1  61, 3
2       2 112, 3
3       3  30, 2
4       4     21

想要的结果应该是这样的:

  Group.1  x.0  x.1
1       1  61   3
2       2 112   3
3       3  30   2
4       4  21   0

我该如何解决这个问题?

我们需要将 'var' 创建为 factor,并将 levels 指定为 0 和 1。这将确保如果特定列的计数为 0,则显示作为 0

var <- factor(var, levels = 0:1)
out <- aggregate(var, list(group), FUN = table)
out
#   Group.1 x.0 x.1
#1       1  29  21
#2       2  29  21
#3       3  23  27
#4       4   0  50

或者使用formula方法

out <- aggregate(var ~ group, FUN = table)

请注意,这些将导致 matrix 'x' 包含两列。为了有常规的 data.frame

do.call(data.frame, out)

或者以防万一,我们也可以得到 1 的 sum 然后 reshape

reshape(aggregate(cbind(n = rep(1, length(group))) ~ 
  group+ var, FUN = sum), idvar = 'group', direction = 'wide', timevar = 'var')

如果我们使用 tidyverse,而不是对 class 进行任何更改,即转换为 factor,请使用两个变量执行 count,然后 spread 它变成 'wide' 格式

library(tidyverse)
tibble(var, group) %>% 
    count(var, group) %>%
    spread(var, n, fill = 0)
# A tibble: 4 x 3
#  group   `0`   `1`
#  <int> <dbl> <dbl>
#1     1    29    21
#2     2    29    21
#3     3    23    27
#4     4     0    50

数据

set.seed(24)
var <- sample(0:1, 200, replace = TRUE)
var[151:200] <- 1
group <- rep(1:4, each = 50)