Calculating mean, median, and mode with base R

Altogether, the mean, median, and mode are the most popular measures of central tendency. They kind of tell us where the distribution is centered. The following code block shows how to calculate the first two of them:

mean(small_sample, na.rm = T)# outputs [1] 7.546716mean(big_sample, na.rm = T)# outputs [1] 9.97051median(small_sample, na.rm = T)# outputs [1] 8.449614median(big_sample, na.rm = T)# outputs [1] 9.979968
To keep it simple, the arithmetic mean is the sum of all values divided by the number of observations. Median is the middle observation (center) of a sorted sample and mode is the value (or values) that are most frequent in the dataset (if there is one).

The mean() and median() ...

Get Hands-On Data Science with R now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.