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 c(1, 2, 3, 4, 5, 6) is 1:6. Try changing the first line to dice_values = 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 (as an interval) respectively. Remember that the sample standard deviation or variance formulas have a denominator of \(n - 1\) rather than \(n\).

Mode

Mode is a less common calculation and R does not have a function to calculate it.

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.

Which value appears the most times?
Select an item

Calculating summary statistics in Desmos

In Desmos, you can enter a list of numbers like \[ a = [1, 2, 3, 4, 5, 6] \text{ or } a = [1...6]. \] (Variables in Desmos are limited to a single letter.)

Statistic R Desmos
Mean mean(a) mean(a)
Median median(a) median(a)
(Sample) Variance var(a) var(a)
(Sample) Standard Deviation sd(a) stdev(a)
Range (as an interval) range(a) (min(a), max(a))