Calculate Skewness in R

Your goal

You need to calculate the skewness 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")

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)
> skewness(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 approximately 0.3939.

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

Histogram for age
Histogram for age