Zacznij terazZacznij za darmo

Harmonic mean

The harmonic mean is the reciprocal of the arithmetic mean of the reciprocal of the data. That is

$$ \text{harmonic_mean}(x) = 1 / \text{arithmetic_mean}(1 / x) $$

The harmonic mean is often used to average ratio data. You'll be using it on the price/earnings ratio of stocks in the Standard and Poor's 500 index, provided as std_and_poor500. Price/earnings ratio is a measure of how expensive a stock is.

The dplyr package is loaded.

To ćwiczenie jest częścią kursu

Introduction to Writing Functions in R

Zobacz kurs

Interaktywne ćwiczenie praktyczne

Spróbuj tego ćwiczenia, uzupełniając ten przykładowy kod.

# Look at the Standard and Poor 500 data
glimpse(std_and_poor500)

# Write a function to calculate the reciprocal
___ <- ___ {
  ___
}
Edytuj i uruchom kod