3  Calculating Mean, Median, etc. in R

We’ve seen the mean() function before in Activity 1. This calculates the mean of a set of data. For instance

Tip

The short-hand for a consecutive list like 1, 2, 3, 4, 5, 6 is 1:6. Try changing the dice_values variable to 1:20 and get the average dice value on a 20 sided die—common in tabletop games like Dungeons & Dragons.

What is the average roll on a 20 sided die?

Likewise, the median(), sd(), var(), and range() functions exists to calculate the median, (sample) standard deviation, (sample) variance, and range respectively. Remember that the sample s.d. or var formulas have a denominator of \(n - 1\) rather than \(n\).

3.1 Mode

Mode is a less common calculation and the mode() function in R does not calculate the mode but does something much different having to do with programming internals.

Instead, we can calculate the mode as we did in Activity 1 by using the table() function and then reading the output to determine which data point occurred the most.