Learn

Finding the mode of a dataset becomes increasingly time-consuming as the size of your dataset increases — imagine finding the mode of a dataset with 10,000 observations.

The R package DescTools includes a handy Mode() function which can do the work of finding the mode for us. In the example below, we use Mode() to calculate the mode of a dataset with ten values:

Example: One Mode

library(DescTools) example_data <- c(24, 16, 12, 10, 12, 28, 38, 12, 28, 24) example_mode <- Mode(example_data)

The code above calculates the mode of the values in example_data and saves it to example_mode.

The result of Mode() is a vector with the mode value:

>>> example_mode [1] 12

Example: Two Modes

If there are multiple modes, the Mode() function will return them as a vector.

Let’s look at a vector with two modes, 12 and 24:

example_data = c(24, 16, 12, 10, 12, 24, 38, 12, 28, 24) example_mode = Mode(example_data)

The result is:

>>> example_mode [1] 12 24

Instructions

1.

We have already imported the DescTools library for you.

Delete the current value set to mode_age.

Find the mode of the observations in the author_ages array. Save the result to mode_age.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?