開始使用免費開始

一個或多個變數的特定統計量

如同你先前看到的,你可以使用 summary()psych::describe()Hmisc::describe(),一次取得資料集中一個或多個變數的多種描述統計。你也已在前面的練習中,用 mean()median()sd() 等函式計算過單一統計量。

不過,dplyr 套件提供的 summarise() 函式,單獨使用或搭配 across(everything(), list()) 語法,都能同時為一個或多個變數計算多個你感興趣的自訂統計量。

abaloneKeep 資料集與 dplyr 套件已為你載入。

本練習屬於課程

給 SAS 使用者的 R

檢視課程

練習說明

  • 使用 summary()abaloneKeep 資料集中取得去殼重量(shucked weight)與整體重量(whole weight)的摘要統計。
  • 使用 dplyr 套件的 summarise() 函式計算 length 的平均數與標準差。請將輸出統計命名為 mean_length(平均數)與 sd_length(標準差)。
  • 使用 dplyr 套件的 summarise(across(everything(), list())) 語法,計算 height 與 diameter 的平均數與標準差。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Run summary() for shuckedWeight and wholeWeight
abaloneKeep %>%
  select(___, ___) %>%
  ___

# Get mean and sd for length
abaloneKeep %>%
  ___(___ = mean(___),
      ___ = sd(___))

# Get mean and sd for height and diameter
abaloneKeep %>%
  ___ %>%
  summarise(across(everything(),list(___ = ~ mean(.x),
                                     ___ = ~ sd(.x))))
編輯並執行程式碼