依群組的摘要統計
延續上一個練習,這題你會繼續搭配 group_by() 使用 dplyr::summarise() 與 summarise(across(everything(), list())) 語法,來對特定變數依你感興趣的群組(例如 sex 與 adult 類別)計算自訂統計量。
abaloneKeep 資料集與 dplyr 套件已為你載入。
本練習屬於課程
給 SAS 使用者的 R
練習說明
- 使用
summarise()搭配group_by(),依分組變數取得統計量。 - 針對 whole weight 依 adult 分組,計算中位數、第 25 與第 75 百分位數,並將輸出命名為
median_wweight、q1_wweight、q3_wweight。 - 針對 height 與 diameter 依 sex 分組,計算平均數與標準差。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Get median, 25th, 75th percentiles for wholeWeight by adult
abaloneKeep %>%
group_by(___) %>%
select(___) %>%
summarise(median_wweight = median(___),
q1_wweight = quantile(___, probs = ___),
q3_wweight = quantile(___, probs = ___))
# Get mean and sd for height and diameter by sex
abaloneKeep %>%
group_by(___) %>%
select(___) %>%
summarise(across(everything(),list(___ = ~ mean(.x),
___ = ~ sd(.x))))