VOOZH about

URL: https://www.geeksforgeeks.org/r-language/calculate-the-median-absolute-deviation-in-r-programming-mad-function/

⇱ Calculate the Median Absolute Deviation in R Programming - mad() Function - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 15 Jul, 2025

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. Because it is resistant to outliers and extreme values, the MAD can serve as a robust alternative to standard deviation.

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

MAD = median (|xi - x)|

where:

  • xi = Each observation in the dataset is represented.
  • The dataset's median is represented as median(x).

Syntax:

mad(x)

Parameters:

  • x : A vector of numeric values.

Calculate MAD for vectors

We can calculate the Median Absolute Deviation for vectors.

Example 1:


Output
[1] 2.9652

Example 2:


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.


Output
[1] 0.44478

Calculate MAD for multiple columns in a data

To find the MAD for multiple columns, we can apply the apply() or sapply() function together with the mad() function. Here's how you can find the MAD for all the columns except the categorical ones in the iris dataset:

Output:

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

The sapply() function uses the mad() function for every column in the dataset except for the categorical Species column. The MAD value for every numerical column is returned.

Related Articles:

Comment
Article Tags:

Explore