調和平均數
調和平均數是資料取倒數後的算術平均數再取倒數。也就是說:
$$ \text{harmonic_mean}(x) = 1 / \text{arithmetic_mean}(1 / x) $$
調和平均數常用來對比率型資料做平均。你將用它來計算標準普爾 500 指數中股票的本益比(price/earnings ratio),資料已提供為 std_and_poor500。本益比用來衡量一檔股票的相對昂貴程度。
已載入 dplyr 套件。
本練習屬於課程
R 函式撰寫入門
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Look at the Standard and Poor 500 data
glimpse(std_and_poor500)
# Write a function to calculate the reciprocal
___ <- ___ {
___
}