总结后避免丢失行

时间:2015-01-03 15:36:48

标签: r dplyr summarization

我在Windows上使用RStudio版本0.98.1028。使用函数sum()汇总包含dplyr的多级数据框,我丢失了一行sum = 0。换句话说,如果我的原始数据框类似于

group <- as.factor(rep(c('X', 'Y'), each = 1, times = 6))
type <- as.factor(rep(c('a', 'b'), each = 2, times = 3))
day <- as.factor(rep(1:3, each = 4))

df = data.frame(type = type, day = day, value = abs(rnorm(12)))
df = df[day != 1 | type != 'a',]

我总结了它

df1 = df %>%
    group_by(day, type) %>%
    summarise(sum = sum(value))

然后我得到一个缺失的行,这是我想要的day = 1type = a之间的互动(即使它是0 ...)

提前致谢!

EB

1 个答案:

答案 0 :(得分:0)

您可以尝试left_join

library(dplyr)
left_join(expand.grid(type=unique(df$type), day=unique(df$day)), df1) %>%
                            group_by(day, type) %>%
                            summarise(sum=sum(value, na.rm=TRUE))
#  day type       sum
#1   1    a 0.0000000
#2   1    b 0.5132914
#3   2    a 1.2482210
#4   2    b 0.9232343
#5   3    a 2.0381779
#6   3    b 0.7558351

其中df1

 df1 <- df[day != 1 | type != 'a',]