Calculate Skewness in R
You need to calculate the skewness of a numerical dataset in R.
We'll use the
moments package for this. Install it as follows:
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
> library(moments) > library(readr) > titanic <- read_csv("titanic-full.csv") > age <- as.numeric(titanic$age) > skewness(age, na.rm=TRUE)
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: