第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

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

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

胡說(shuō)叔叔 2019-10-17 10:11:28
summarise與with plyr的ddply函數(shù)一起使用時(shí),默認(rèn)情況下會(huì)刪除空類別。您可以通過(guò)添加更改此行為.drop = FALSE。但是,當(dāng)summarise與結(jié)合使用時(shí),這是行不通的dplyr。還有另一種方法可以在結(jié)果中保留空類別嗎?這是偽數(shù)據(jù)的示例。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不完全是我的期望。有沒(méi)有dplyr達(dá)到同樣的結(jié)果的方法.drop=FALSE的plyr?
查看完整描述

3 回答

?
青春有我

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超8個(gè)贊

由于dplyr 0.8 group_by獲得了.drop滿足您要求的參數(shù):


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當(dāng)一個(gè)或多個(gè)分組變量未編碼為因素時(shí),使用可能會(huì)產(chǎn)生意外結(jié)果。請(qǐng)參閱以下示例:


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)


查看完整回答
反對(duì) 回復(fù) 2019-10-17
?
MMMHUHU

TA貢獻(xiàn)1834條經(jīng)驗(yàn) 獲得超8個(gè)贊

這個(gè)問(wèn)題仍然存在,但是與此同時(shí),尤其是因?yàn)槟臄?shù)據(jù)已經(jīng)被考慮到了,您可以使用complete“ tidyr”獲取可能要查找的內(nèi)容:


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

如果您希望替換值為零,則需要使用進(jìn)行指定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


查看完整回答
反對(duì) 回復(fù) 2019-10-17
  • 3 回答
  • 0 關(guān)注
  • 977 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)