Today, we will discuss the median function in the R programming language together. This tutorial demonstrates the median computation process in the R language.
What is the median function in R?
The sample median is determined using the median function. When the data is sorted in ascending order, the median is the value that falls in the center. By including it inside the aggregate function, the median of a group can also be determined using the median function.
Syntax:
median(data, na.rm = FALSE, ...)
Parameter:
- data: The data is a vector, …
How to use the median function in R?
Primary Application of the median function
Here, we need to create a vector to apply the median function as follows:
# Create a vector data <- c(1, 7, 9, 7, 5, 3, 2, 3, 5) # View a vector cat("The vector is:", data)
Output
The vector is: 1 7 9 7 5 3 2 3 5
Now, we will apply the median function for this vector to calculate the median.
# Create a vector data <- c(1, 7, 9, 7, 5, 3, 2, 3, 5) # Calculate median res <- median(data) cat("The median is:", res)
Output
The median is: 5
Calculate the median of the vector with NA values
In this example, we need to create a vector with NA values as follows:
# Create a vector with NA values data <- c(1, 7, NA, 7, NA, 3, NA) # View a vector cat("The vector is:", data)
Output
The vector is: 1 7 NA 7 NA 3 NA
Now, when we run this vector through the median R function, it will return NA values.
# Create a vector with NA values data <- c(1, 7, NA, 7, NA, 3, NA) # Calculate median res <- median(data) cat("The median is:", res)
Output
The median is: NA
It is a good thing that the median function offers the na.rm option, which lets the user ignore any NA values before computing the median:
# Create a vector with NA values data <- c(1, 7, NA, 7, NA, 3, NA) # Calculate median res <- median(data, na.rm = TRUE) cat("The median is:", res)
Output
The median is: 5
Calculate the median of the data frame
Similarly, we will have a data frame as follows:
# Create a data frame df <- data.frame( ID = c(1:5), Math = c(6, 7, 8, 6, 4), English = c(5, 8, 6, 4, 3), Physics = c(8, 9, 7, 9, 7) ) # View a data frame cat("The data frame is:\n") df
Output
The data frame is:
ID Math English Physics
1 1 6 5 8
2 2 7 8 9
3 3 8 6 7
4 4 6 4 9
5 5 4 3 7
If you want to compute the median of the Math column of this data frame, we will apply the median function, read the following code example to understand.
# Create a data frame df <- data.frame( ID = c(1:5), Math = c(6, 7, 8, 6, 4), English = c(5, 8, 6, 4, 3), Physics = c(8, 9, 7, 9, 7) ) # Calculate the median Math column res <- median(df$Math) cat("The median of the Math column is:", res)
Output
The median of the Math column is: 6
Fortunately, if you have a column with NA values, we will use na.rm = TRUE similar to when calculating the median of a vector with NA values. See the code example below.
# Create a data frame with NA values df <- data.frame( ID = c(1:5), Math = c(6, 7, 8, 6, 4), English = c(5, 8, 6, 4, 3), Physics = c(8, 9, NA, 9, NA) ) # Calculate median Math column res <- median(df$Physics, na.rm = TRUE) cat("The median of Physics column is:", res)
Output
The median of Physics column is: 9
Summary
This tutorial shared how to apply the median function in the R language with the vector and data frame. So, if you have any questions, please leave a comment below.
Have a great day!
Maybe you are interested:

Hi, guys! My name’s Scott Miller. My current job is a software developer and I have shared a lot of quality articles about Javascript, C, C++, C#, Python, PHP, R, Java programming languages. I’m hoping they can assist you.
Name of the university: HCMUS
Major: IT
Programming Languages: C, C++, Python, R, Java, JavaScript