Open In App

# Calculate the Median Absolute Deviation in R Programming – mad() Function

The Median Absolute Deviation is calculated in R Language using the mad() function. It is a statistical measurement of a dataset’s dispersion or variability. Due to its resistance to outliers and extreme values, the MAD is a reliable substitute for the standard deviation.

The Median Absolute Deviation (MAD) is calculated using the following formula:

MAD is equal to median(|xi – x)|.

where:

Each observation in the dataset is represented by xi.
The dataset’s median is represented as median(x).

### Calculate MAD for vectors :

We can calculate the Median Absolute Deviation for vectors.

Example 1:

## R

 # R Program to calculate# Median Absolute Deviation # Creating a vectorx <- c(1:9) # Calling mad() Functionmad(x)

Output:

[1] 2.9652

Example 2:

## R

 # R Program to calculate# Median Absolute Deviation # Creating a vectorx <- c(1, 4, 2, 3, 7, 3, 8, 9, 2) # Calling mad() Functionmad(x)

Output:

[1] 1.4826

### Calculate MAD for a single column in a data:

We can calculate MAD for a single column in a data set so we can take the iris dataset.

## R

Output:

[1] 0.44478

### Calculate MAD for multiple columns in a data:

With the help of apply function, we can calculate the Median absolute deviation for multiple columns.