调和平均数
调和平均数是数据倒数的算术平均数的倒数。也就是说:
$$ \text{harmonic_mean}(x) = 1 / \text{arithmetic_mean}(1 / x) $$
调和平均数常用于对比率型数据求平均。您将把它用于标准普尔 500 指数中股票的市盈率,该数据集提供为 std_and_poor500。市盈率用于衡量一只股票的相对"贵"或"便宜"。
已加载 dplyr 包。
本练习是课程的一部分
R 函数编写入门
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Look at the Standard and Poor 500 data
glimpse(std_and_poor500)
# Write a function to calculate the reciprocal
___ <- ___ {
___
}