Calculate the Median Absolute Deviation in R Programming - mad() Function Last Updated : 22 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: R # Creating a vector x <- c(1:9) # Calling mad() Function mad(x) Output[1] 2.9652 Example 2: R # 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 DataWe can calculate MAD for a single column in a data set so we can take the iris dataset. R data(iris) #calculate the mad for single columns. mad(iris$Sepal.Width) Output[1] 0.44478 Calculate MAD for multiple columns in a dataTo 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: R 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 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:R Programming Language – IntroductionStandard Deviation Comment More infoAdvertise with us Next Article Calculate the Median Absolute Deviation in R Programming - mad() Function N nidhi_biet Follow Improve Article Tags : R Language R Math-Function Similar Reads Calculate Arithmetic mean in R Programming - mean() Function mean() function in R Language is used to calculate the arithmetic mean of the elements of the numeric vector passed to it as argument. Syntax: mean(x, na.rm) Parameters: x: Numeric Vector na.rm: Boolean value to ignore NA value Example 1: Python3 1== # R program to calculate # Arithmetic mean of a v 1 min read Calculate Median of elements of a Vector in R Programming - median() Function median() function in R Language is used to calculate the median of the elements of the numeric vector passed as argument. Syntax: median(x, na.rm) Parameters: x: Numeric Vector na.rm: Boolean value to ignore NA Example 1: Python3 1== # R program to calculate median of a vector # Creating vector x1 1 min read Calculate the Weighted Mean in R Programming - weighted.mean() Function weighted.mean() function in R Language is used to compute the weighted arithmetic mean of input vector values. Syntax: weighted.mean(x, weights) Parameters: x: data input vector weights: It is weight of input data. Returns: weighted mean of given values Example 1: r # Create example data x1 <- c( 1 min read Calculate the Mean of each Row of an Object in R Programming â rowMeans() Function rowMeans() function in R Language is used to find out the mean of each row of a data frame, matrix, or array. Syntax: rowMeans(data) Parameter: data: data frame, array, or matrix Example 1 Python3 1== # R program to illustrate # rowMean function # Create example values # Set seed set.seed(1234) # Cr 1 min read Calculate the Interquartile Range in R Programming - IQR() Function The first quartile (Q1) and the third quartile (Q3) of a dataset are represented by the interquartile range (IQR), a statistical measure of statistical dispersion. Particularly for the middle 50% of the sample, it offers insightful information regarding the distribution and variability of the data.H 2 min read Like