While you’ve shown that you can calculate the average yourself, it becomes time-consuming as the size of your dataset increases — imagine adding all of the numbers in a dataset with 10,000 observations.
mean() function can do the work of adding and dividing for you. In the example below, we use
mean() to calculate the average of a dataset with ten values:
example_data <- c(24, 16, 30, 10, 12, 28, 38, 2, 4, 36) example_average <- mean(example_data) print(example_average)
The code above calculates the average of
example_data and saves the value to
example_average. The resulting average of this array is
Use R to calculate the average value of the
author_ages array. Save the result to
average_age and print it.
Does the average age of the authors surprise you? If so, how? Is it older, or younger than you expected?