# 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).

Syntax: mad(x) Parameters: x: Vector

### 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 vector x <- c(1:9)   # Calling mad() Function mad(x)

Output:

[1] 2.9652

Example 2:

## R

 # R Program to calculate # Median Absolute Deviation   # Creating a vector x <- c(1, 4, 2, 3, 7, 3, 8, 9, 2)   # Calling mad() Function mad(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

 #load the dataset data(iris)   #calculate the mad for single columns. mad(iris\$Sepal.Width)

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.

## R

 # load library library(dplyr)   # remove Species column from dataset data=select(iris,-('Species'))   # calculate the mad for all columns sapply(data,mad)

Output:

Sepal.Length  Sepal.Width Petal.Length  Petal.Width
1.03782      0.44478      1.85325      1.03782

We can calculate the mad for multiple columns in a dataset with the help of sapply function. we remove the species column from the data because the mad function only works on numerical columns.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next
Similar Reads