dplyr summarise:等效于“ .drop = FALSE”,以在输出中保留长度为零的组

summarise与with plyr的ddply函数一起使用时,默认情况下会删除空类别。您可以通过添加更改此行为.drop = FALSE。但是,当summarise与结合使用时,这是行不通的dplyr。还有另一种方法可以在结果中保留空类别吗?


这是伪数据的示例。


library(dplyr)


df = data.frame(a=rep(1:3,4), b=rep(1:2,6))


# Now add an extra level to df$b that has no corresponding value in df$a

df$b = factor(df$b, levels=1:3)


# Summarise with plyr, keeping categories with a count of zero

plyr::ddply(df, "b", summarise, count_a=length(a), .drop=FALSE)


  b    count_a

1 1    6

2 2    6

3 3    0


# Now try it with dplyr

df %.%

  group_by(b) %.%

  summarise(count_a=length(a), .drop=FALSE)


  b     count_a .drop

1 1     6       FALSE

2 2     6       FALSE

不完全是我的期望。有没有dplyr达到同样的结果的方法.drop=FALSE的plyr?


胡说叔叔
浏览 926回答 3
3回答

青春有我

由于dplyr 0.8 group_by获得了.drop满足您要求的参数:df = data.frame(a=rep(1:3,4), b=rep(1:2,6))df$b = factor(df$b, levels=1:3)df %>%&nbsp; group_by(b, .drop=FALSE) %>%&nbsp; summarise(count_a=length(a))#> # A tibble: 3 x 2#>&nbsp; &nbsp;b&nbsp; &nbsp; &nbsp;count_a#>&nbsp; &nbsp;<fct>&nbsp; &nbsp;<int>#> 1 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;6#> 2 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;6#> 3 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0@Moody_Mudskipper的答案还有另一条注释:.drop=FALSE当一个或多个分组变量未编码为因素时,使用可能会产生意外结果。请参阅以下示例:library(dplyr)data(iris)# Add an additional level to Speciesiris$Species = factor(iris$Species, levels=c(levels(iris$Species), "empty_level"))# Species is a factor and empty groups are included in the outputiris %>% group_by(Species, .drop=FALSE) %>% tally#>&nbsp; &nbsp;Species&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;n#> 1 setosa&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;50#> 2 versicolor&nbsp; &nbsp; &nbsp;50#> 3 virginica&nbsp; &nbsp; &nbsp; 50#> 4 empty_level&nbsp; &nbsp; &nbsp;0# Add character columniris$group2 = c(rep(c("A","B"), 50), rep(c("B","C"), each=25))# Empty groups involving combinations of Species and group2 are not included in outputiris %>% group_by(Species, group2, .drop=FALSE) %>% tally#>&nbsp; &nbsp;Species&nbsp; &nbsp; &nbsp;group2&nbsp; &nbsp; &nbsp;n#> 1 setosa&nbsp; &nbsp; &nbsp; A&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;25#> 2 setosa&nbsp; &nbsp; &nbsp; B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;25#> 3 versicolor&nbsp; A&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;25#> 4 versicolor&nbsp; B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;25#> 5 virginica&nbsp; &nbsp;B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;25#> 6 virginica&nbsp; &nbsp;C&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;25#> 7 empty_level <NA>&nbsp; &nbsp; &nbsp; &nbsp;0# Turn group2 into a factoriris$group2 = factor(iris$group2)# Now all possible combinations of Species and group2 are included in the output,&nbsp;#&nbsp; whether present in the data or notiris %>% group_by(Species, group2, .drop=FALSE) %>% tally#>&nbsp; &nbsp; Species&nbsp; &nbsp; &nbsp;group2&nbsp; &nbsp; &nbsp;n#>&nbsp; 1 setosa&nbsp; &nbsp; &nbsp; A&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;25#>&nbsp; 2 setosa&nbsp; &nbsp; &nbsp; B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;25#>&nbsp; 3 setosa&nbsp; &nbsp; &nbsp; C&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0#>&nbsp; 4 versicolor&nbsp; A&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;25#>&nbsp; 5 versicolor&nbsp; B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;25#>&nbsp; 6 versicolor&nbsp; C&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0#>&nbsp; 7 virginica&nbsp; &nbsp;A&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0#>&nbsp; 8 virginica&nbsp; &nbsp;B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;25#>&nbsp; 9 virginica&nbsp; &nbsp;C&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;25#> 10 empty_level A&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0#> 11 empty_level B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0#> 12 empty_level C&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0Created on 2019-03-13 by the reprex package (v0.2.1)

MMMHUHU

这个问题仍然存在,但是与此同时,尤其是因为您的数据已经被考虑到了,您可以使用complete“ tidyr”获取可能要查找的内容:library(tidyr)df %>%&nbsp; group_by(b) %>%&nbsp; summarise(count_a=length(a)) %>%&nbsp; complete(b)# Source: local data frame [3 x 2]#&nbsp;#&nbsp; &nbsp; &nbsp; &nbsp; b count_a#&nbsp; &nbsp;(fctr)&nbsp; &nbsp;(int)# 1&nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp;6# 2&nbsp; &nbsp; &nbsp; 2&nbsp; &nbsp; &nbsp; &nbsp;6# 3&nbsp; &nbsp; &nbsp; 3&nbsp; &nbsp; &nbsp; NA如果您希望替换值为零,则需要使用进行指定fill:df %>%&nbsp; group_by(b) %>%&nbsp; summarise(count_a=length(a)) %>%&nbsp; complete(b, fill = list(count_a = 0))# Source: local data frame [3 x 2]#&nbsp;#&nbsp; &nbsp; &nbsp; &nbsp; b count_a#&nbsp; &nbsp;(fctr)&nbsp; &nbsp;(dbl)# 1&nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp;6# 2&nbsp; &nbsp; &nbsp; 2&nbsp; &nbsp; &nbsp; &nbsp;6# 3&nbsp; &nbsp; &nbsp; 3&nbsp; &nbsp; &nbsp; &nbsp;0
打开App,查看更多内容
随时随地看视频慕课网APP