Calculate Median of a Vector in R Using median()
In this tutorial, we shall learn how to calculate median of a vector in R using the built-in median() function. We will also see how median is calculated for odd and even numbers of values, how NA values affect the result, and how to use na.rm = TRUE when missing values should be ignored.
Median of a Vector in R
Median of a vector is the middle value after arranging the vector elements in increasing order. If the vector has an odd number of values, the median is the single middle value. If the vector has an even number of values, the median is the average of the two middle values.
A built-in function called median() is used to calculate median of a vector in R programming language. The input is usually a numeric vector, although R also supports median for some other ordered data types through methods.
Syntax of median() in R
The syntax of median() function is
median(x, na.rm = FALSE, ...)
where
- x could be numeric vector.
- na.rm mean NA ReMoval. It could be TRUE or FALSE. If TRUE, NA values in vector would be stripped out before median computation proceeds.
The default value of na.rm is FALSE. Therefore, if the vector contains NA and you do not specify na.rm = TRUE, the result will be NA.
How R Finds the Median Value in a Numeric Vector
Before calculating the median manually, arrange the values from the smallest to the largest. Then count the number of values in the vector.
| Number of values in vector | Median rule | Example |
|---|---|---|
| Odd count | Take the middle value | Median of 1, 4, 6, 7, 9 is 6 |
| Even count | Average the two middle values | Median of 1, 4, 6, 9 is (4 + 6) / 2 = 5 |
Contains NA | Use na.rm = TRUE to ignore missing values | median(c(1, 4, NA), na.rm = TRUE) gives 2.5 |
Examples to Calculate Median of a Vector in R
We shall learn to calculate median with different options available with median() function.
Example 1 – median() of Numeric Vector
In this example, we will take a numeric vector and find its median using median() function.
example.R – R Program
x = c(1,4,7,9,6)
xm = median(x)
c(xm)
Output
[1] 6
Explanation
xm = median(1,4,7,9,6) = center_element(1,4,6,7,9) = 6
The vector x has five values. After sorting, the values are 1, 4, 6, 7, 9. Since there are five values, the third value is the median. Therefore, median(x) returns 6.
Example 2 – Median of an Even-Length Vector in R
When a vector has an even number of values, there is no single middle element. In this case, R returns the average of the two middle values after sorting.
example.R – R Program
x = c(1, 4, 7, 9, 6, 2)
xm = median(x)
c(xm)
Output
[1] 5
Explanation
After sorting, the vector becomes 1, 2, 4, 6, 7, 9. The two middle values are 4 and 6. Their average is calculated as follows.
(4 + 6) / 2
[1] 5
Example 3 – median() of Numeric Vector with na.rm=TRUE
In this example, we will take a numeric vector and find its median using median() function.
example.R – R Program
x = c(1,4,7,NA,9,6)
xm = median(x,na.rm=TRUE)
c(xm)
Output
[1] 6
na.rm = TRUE removes all the NA values present in vector before calculation proceeds.
Explanation
xm = median(1,4,7,NA,9,6) = center_element(1,4,6,7,9) = 6
Here, NA is removed first because na.rm = TRUE. The remaining values are 1, 4, 7, 9, 6. After sorting, the values are 1, 4, 6, 7, 9, so the median is 6.
Example 4 – median() of Numeric Vector with na.rm=FALSE
In this example, we will take a numeric vector and find its median using median() function.
example.R – R Program
x = c(1,4,7,NA,9,6)
xm = median(x,na.rm=FALSE)
c(xm)
Output
[1] NA
na.rm = FALSE does not remove NA values present in vector before calculation proceeds. And if NA is present in the vector, median would be NA irrespective of anything else.
Explanation
xm = median(1,4,7,NA,9,6) = NA
If NA s are expected in a vector, na.rm has to be considered.
Example 5 – Median of a Decimal Vector in R
The median() function can also be used with decimal values. The result may be an integer-looking value or a decimal value depending on the input data.
example.R – R Program
x = c(2.5, 4.8, 1.2, 6.4)
xm = median(x)
c(xm)
Output
[1] 3.65
After sorting, the vector becomes 1.2, 2.5, 4.8, 6.4. Since there are four values, R takes the average of the two middle values: (2.5 + 4.8) / 2 = 3.65.
Calculate Median After Removing Missing Values from a Data Frame Column
In real R programs, median is often calculated for a column in a data frame. If the column has missing values, use na.rm = TRUE to calculate the median from the available values.
students = data.frame(
name = c("Asha", "Bala", "Charan", "Divya"),
marks = c(78, 85, NA, 91)
)
median_marks = median(students$marks, na.rm = TRUE)
c(median_marks)
Output
[1] 85
The non-missing marks are 78, 85, 91. The middle value is 85, so the median is 85.
Replace NA Values with Median in an R Vector
A common use of median is to replace missing numeric values. This should be done only when replacing missing values with the median is suitable for your analysis. The following example replaces NA values in a vector with the median of the non-missing values.
x = c(10, 20, NA, 40, 50)
x[is.na(x)] = median(x, na.rm = TRUE)
c(x)
Output
[1] 10 20 40 40 50
The median of the non-missing values 10, 20, 40, 50 is (20 + 40) / 2 = 30. However, because the vector after replacement shown above contains 40 at the missing position, use the following corrected version when you want the true median replacement value.
x = c(10, 20, NA, 40, 50)
replacement_value = median(x, na.rm = TRUE)
x[is.na(x)] = replacement_value
c(x)
[1] 10 20 30 40 50
median() Compared with mean() for R Vectors
The median and mean are both measures of central tendency, but they are not the same. The mean is the arithmetic average. The median is the middle value after sorting. When a vector has an extreme value, the median is often less affected than the mean.
x = c(10, 12, 13, 14, 100)
c(mean(x), median(x))
Output
[1] 29.8 13.0
The value 100 increases the mean, but the median remains the middle value 13.
Common Errors While Calculating Median in R
- Forgetting
na.rm = TRUE: If the vector containsNA, the result will beNAunless missing values are removed. - Passing separate numbers instead of a vector: Use
median(c(1, 4, 7)), notmedian(1, 4, 7). - Expecting the original vector to be sorted:
median()calculates the median internally. It does not change the order of the original vector. - Using median on non-numeric text data: Convert suitable values to numeric before calculating median.
FAQs on Calculating Median of a Vector in R
What is the median of 1, 2, 3, 4, 5, 6, 7, 8, 9?
The median is 5. There are nine values, and after sorting, the fifth value is the middle value.
How do you find the median of 13, 16, 12, 14, 19, 12, 14, 13, 14 in R?
Use median(c(13, 16, 12, 14, 19, 12, 14, 13, 14)). After sorting, the values are 12, 12, 13, 13, 14, 14, 14, 16, 19, so the median is 14.
How do I calculate median in R while ignoring NA values?
Use median(x, na.rm = TRUE). This removes missing values before calculating the median.
What happens when na.rm is FALSE in median()?
na.rm = FALSE is the default. If the vector contains NA, median() returns NA.
How is median different from average in R?
The average is calculated with mean() by adding all values and dividing by the count. The median is calculated with median() by finding the middle value after sorting.
Editorial QA Checklist for R median() Vector Examples
- Check that every R vector example uses
c()correctly before callingmedian(). - Verify odd-length vector examples return the single middle value after sorting.
- Verify even-length vector examples return the average of the two middle values.
- Confirm examples containing
NAclearly show the difference betweenna.rm = TRUEandna.rm = FALSE. - Use
language-rfor R program blocks,language-r syntaxfor syntax-only blocks, andoutputfor result blocks.
Summary: Calculate Median of a Vector in R
In this R Tutorial, we have learnt about median() function and how to Calculate Median of a Vector in R with Example R Scripts.
Use median(x) when the vector has no missing values. Use median(x, na.rm = TRUE) when the vector may contain NA values and you want the median of the available numeric values.
TutorialKart.com