Calculate Kurtosis in R

Your goal

You need to calculate the kurtosis of a numerical dataset in R.

Step-by-step tutorial

We'll use the moments package for this. Install it as follows:

> install.packages("moments")

The moments package supports a couple of different kurtosis calculations, both involving estimating kurtosis from samples. One is an estimator for Pearson's (non-excess) kurtosis by way of the kurtosis function. The other is an estimator for Geary's kurtosis using the geary function. Here we'll use kurtosis.

For data, let's use the Titanic dataset. You can use readr's read_csv function to read the file. We'll calculate the skewness of the age column. Note that in the original dataset this variable has some ? values, so it reads as character data. We need to remove those and convert the column to numeric data.

> library(moments)
> library(readr)
> titanic <- read_csv("titanic-full.csv")
> age <- as.numeric(titanic$age)
> kurtosis(age, na.rm=TRUE)

We use na.rm=TRUE to remove NAs from the data. Alternatively, we could have used age <- na.omit(as.numeric(titanic$age)) to remove them.

The result is 3.144524.

For visualization, we can generate a histogram using hist as well:

Histogram for age
Histogram for age