# Median in R (5 Examples)

This tutorial shows how to **compute the median** in R.

The article is mainly based on the median() function. So let’s have a look at the basic R syntax and the definition of the median function first:

**Basic R Syntax of median():**

median(x) |

median(x)

**Definition of median():**

The median R function computes the sample median of a numeric input vector.

The R tutorial contains **five examples** for the application of the median function in R.

Let’s dive in!

## Example 1: Basic Application of median() in R

Before we can apply the median function, we need to create some example data. Consider the following numeric vector:

x1 <- c(8, 5, 3, 7, 8, 1, 6, 5) # Create example vector |

x1 <- c(8, 5, 3, 7, 8, 1, 6, 5) # Create example vector

We can now use the median R function to compute the median of our example vector:

median(x1) # Apply median function # 5.5 |

median(x1) # Apply median function # 5.5

As you can see based on the RStudio console output, the median of our example vector is 5.5.

**Note:** Our example vector has an even length, resulting in a median value that does not exist in our example vector. The value 5.5 is the mean of the two middle values (i.e. 5 and 6). Learn more about the concept of even and uneven input vectors when calculating the median here.

## Example 2: Compute Median of Vector with NAs

A common issue in survey data is the occurrence of NAs (i.e. missing values). Let’s add such an NA value to our example vector:

x2 <- c(8, 5, 3, 7, 8, 1, 6, 5, NA) # Example vector with NA |

x2 <- c(8, 5, 3, 7, 8, 1, 6, 5, NA) # Example vector with NA

If we now apply the median R function to this vector, the RStudio console returns NA:

median(x2) # Apply median function # NA |

median(x2) # Apply median function # NA

Fortunately, the median function provides the option na.rm, which enables the user to exclude all NA values before the computation of the median:

median(x2, na.rm = TRUE) # median function with na.rm # 5.5 |

median(x2, na.rm = TRUE) # median function with na.rm # 5.5

## Example 3: Median of Column in Real Data Set

We can also compute the median of a column of a data matrix. For this example, I’m going to use the Iris Flower data. Let’s load and inspect the data structure:

data(iris) # Load iris data head(iris) # Head of iris data |

data(iris) # Load iris data head(iris) # Head of iris data

**Table 1: First 6 Rows of Iris Data Matrix.**

We can apply the median function to the first column of this data frame as follows:

median(iris$Sepal.Length) # Median of first column # 5.8 |

median(iris$Sepal.Length) # Median of first column # 5.8

The median of the column Sepal.Length is 5.8.

**Note:** We can also calculate the median across all columns and rows of our data with the colMedians and colRows functions. You can learn more about the function in this R programming tutorial.

## Example 4: Median by Group

The Iris Flower data set also contains a group indicator (i.e. the column Species). We can combine the aggregate function with the median function to get the median by group:

aggregate(iris$Sepal.Length, # Median by group list(iris$Species), median) # Group.1 x # setosa 5.0 # versicolor 5.9 # virginica 6.5 |

aggregate(iris$Sepal.Length, # Median by group list(iris$Species), median) # Group.1 x # setosa 5.0 # versicolor 5.9 # virginica 6.5

The group setosa has a median of 5.0; the group versicolor has a median of 5.9; and the group virginica has a median of 6.5.

## Example 5: Visualize Median in Boxplot & Histogram

In statistical research, the median is often used in figures and graphics. Let’s create another example vector with larger sample size to get some nicer looking graphs:

set.seed(1717) # Set seed x3 <- rpois(1000, 3) # Create larger example vector |

set.seed(1717) # Set seed x3 <- rpois(1000, 3) # Create larger example vector

Medians are typically visualized in boxplots, as you can see based on the following R syntax and Figure 1:

boxplot(x3) # Boxplot with median text(x = 1, y = 3.25, # Add text to boxplot "Median of x3", col = "red") |

boxplot(x3) # Boxplot with median text(x = 1, y = 3.25, # Add text to boxplot "Median of x3", col = "red")

**Figure 1: Boxplot with Median in R.**

However, we could also draw a median line to other types of plots such as barcharts, scatterplots, histograms and so on. The following R code and Figure 2 show a median line drawn to a histogram:

hist(x3) # Histogram in R abline(v = median(x3), # Add median to histogram col = "red", lwd = 3) |

hist(x3) # Histogram in R abline(v = median(x3), # Add median to histogram col = "red", lwd = 3)

text(x = 5, y = 200, # Add text to histogram

“Median of x3”,

col = “red”)

**Figure 2: Histogram with Vertical Median Line in R.**

## Further Resources & Summary

This tutorial illustrated how to use the median in different data scenarios in the R programming language.

However, in case you want to know about the theoretical research concept of the median, you could have a look at the following YouTube video of the mathantics channel.

The speaker of the video does not only explain the median, but also of the related concepts of the mode and mean. If you are not sure which metric to use for the calculation of an average, this video may be a good help.

Furthermore, you might want to have a look at some of the other R tutorials of this website:

- Mean in R
- Mode in R
- colSums, rowSums, colMeans, rowMeans, colMedians & rowMedians in R
- Median Absolute Deviation in R
- Mean by Group in R
- The R Programming Language

I hope that you at this point know how to use the median command in R. However, if you have any additional questions or comments, don’t hesitate let me know in the comments section below. In addition, you might want to subscribe to my email newsletter for more R tutorials.

**5**/

**5**(

**3**votes )

### Subscribe to my free statistics newsletter:

### R Tutorials

abs Function in R

all & any R Functions

Set Aspect Ratio of Plot

attach & detach R Functions

attr, attributes & structure in R

cbind R Command

Change ggplot2 Legend Title

Character to Numeric in R

Check if Object is Defined

col & row sums, means & medians

Complete Cases in R

Concatenate Vector of Strings

Convert Date to Weekday

cumsum R Function

Data Frame Column to Numeric

diff Command in R

difftime R Function

dim Function in R

dir R Function

Disable Scientific Notation

Draw Segments in R

droplevels R Example

Evaluate an Expression

Extract Characters from String

Factor to Numeric in R

Format Decimal Places

get, get0 & mget in R

is.na R Function

is.null Function in R

jitter R Function

Join Data with dplyr Package

length Function in R

lowess R Smoothing Function

max and min Functions in R

NA Omit in R

nchar R Function

ncol Function in R

nrow Function in R

outer Function in R

pairs & ggpairs Plot

parse, deparse & R expression

paste & paste0 Functions in R

pmax and pmin R Functions

polygon Plots in R

pretty R Function

R Find Missing Values

R Functions List (+ Examples)

R NA – Values

R Replace NA with 0

rbind & rbind.fill in R

Read Excel Files in R

readLines, n.readLines & readline

Remove Element from List

Remove Legend in ggplot2

Rename Column Name in R

Replace Last Comma of String

rev R Command

Round Numeric Data in R

Save & Load RData Workspace

scan R Function

setdiff R Function

setNames vs. setnames in R

sink Command in R

Sort, Order & Rank Data in R

sprintf Function in R

Square Root in R

str_c Function of stringr Package

str_sub Function of stringr Package

strptime & strftime Functions

substr & substring R Commands

sweep R Function

Transform Data Frames

union Function in R

unlist in R

weekdays, months, quarters & julian in R

with & within R Functions

Write Excel File in R