亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

dplyr summarise:等效于“ .drop = FALSE”,以在輸出中保留長度為零的組

dplyr summarise:等效于“ .drop = FALSE”,以在輸出中保留長度為零的組

胡說叔叔 2019-10-17 10:11:28
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$adf$b = factor(df$b, levels=1:3)# Summarise with plyr, keeping categories with a count of zeroplyr::ddply(df, "b", summarise, count_a=length(a), .drop=FALSE)  b    count_a1 1    62 2    63 3    0# Now try it with dplyrdf %.%  group_by(b) %.%  summarise(count_a=length(a), .drop=FALSE)  b     count_a .drop1 1     6       FALSE2 2     6       FALSE不完全是我的期望。有沒有dplyr達到同樣的結果的方法.drop=FALSE的plyr?
查看完整描述

3 回答

?
青春有我

TA貢獻1784條經驗 獲得超8個贊

由于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 %>%

  group_by(b, .drop=FALSE) %>%

  summarise(count_a=length(a))


#> # A tibble: 3 x 2

#>   b     count_a

#>   <fct>   <int>

#> 1 1           6

#> 2 2           6

#> 3 3           0

@Moody_Mudskipper的答案還有另一條注釋:.drop=FALSE當一個或多個分組變量未編碼為因素時,使用可能會產生意外結果。請參閱以下示例:


library(dplyr)

data(iris)


# Add an additional level to Species

iris$Species = factor(iris$Species, levels=c(levels(iris$Species), "empty_level"))


# Species is a factor and empty groups are included in the output

iris %>% group_by(Species, .drop=FALSE) %>% tally


#>   Species         n

#> 1 setosa         50

#> 2 versicolor     50

#> 3 virginica      50

#> 4 empty_level     0


# Add character column

iris$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 output

iris %>% group_by(Species, group2, .drop=FALSE) %>% tally


#>   Species     group2     n

#> 1 setosa      A         25

#> 2 setosa      B         25

#> 3 versicolor  A         25

#> 4 versicolor  B         25

#> 5 virginica   B         25

#> 6 virginica   C         25

#> 7 empty_level <NA>       0


# Turn group2 into a factor

iris$group2 = factor(iris$group2)


# Now all possible combinations of Species and group2 are included in the output, 

#  whether present in the data or not

iris %>% group_by(Species, group2, .drop=FALSE) %>% tally


#>    Species     group2     n

#>  1 setosa      A         25

#>  2 setosa      B         25

#>  3 setosa      C          0

#>  4 versicolor  A         25

#>  5 versicolor  B         25

#>  6 versicolor  C          0

#>  7 virginica   A          0

#>  8 virginica   B         25

#>  9 virginica   C         25

#> 10 empty_level A          0

#> 11 empty_level B          0

#> 12 empty_level C          0


Created on 2019-03-13 by the reprex package (v0.2.1)


查看完整回答
反對 回復 2019-10-17
?
MMMHUHU

TA貢獻1834條經驗 獲得超8個贊

這個問題仍然存在,但是與此同時,尤其是因為您的數據已經被考慮到了,您可以使用complete“ tidyr”獲取可能要查找的內容:


library(tidyr)

df %>%

  group_by(b) %>%

  summarise(count_a=length(a)) %>%

  complete(b)

# Source: local data frame [3 x 2]

#        b count_a

#   (fctr)   (int)

# 1      1       6

# 2      2       6

# 3      3      NA

如果您希望替換值為零,則需要使用進行指定fill:


df %>%

  group_by(b) %>%

  summarise(count_a=length(a)) %>%

  complete(b, fill = list(count_a = 0))

# Source: local data frame [3 x 2]

#        b count_a

#   (fctr)   (dbl)

# 1      1       6

# 2      2       6

# 3      3       0


查看完整回答
反對 回復 2019-10-17
  • 3 回答
  • 0 關注
  • 967 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號