# Get Second Lowest & Highest Value in R (2 Examples)

This article explains how to **find the second (and thirdâ€¦) lowest / highest values of a vector or data frame column** in R.

The content of the tutorial looks as follows:

Letâ€™s dive into itâ€¦

## Introduction of Example Data

As a first step, letâ€™s create some example data:

x <- c(5, 9, 1, 0, -3, 2, -5) # Create example vector x # Print example vector # 5 9 1 0 -3 2 -5 |

x <- c(5, 9, 1, 0, -3, 2, -5) # Create example vector x # Print example vector # 5 9 1 0 -3 2 -5

Have a look at the previously shown output of the RStudio console. It reveals that our example data is a numeric vector with a random sequence of values.

## Example 1: Return Second Lowest / Highest Value Using sort & length Functions

The following R programming syntax explains how to extract the second lowest and highest values of our vector using the sort and length functions in R.

First, letâ€™s return the second lowest value of our vector:

sort(x)[2] # Applying sort() function # -3 |

sort(x)[2] # Applying sort() function # -3

The second lowest values is â€“ 3.

Now, letâ€™s find the second highest value of our vector. This time, we also need to use the length function to index our vector properly:

sort(x)[length(x) - 1] # Applying sort() & length() # 5 |

sort(x)[length(x) - 1] # Applying sort() & length() # 5

The second highest value is 5.

Note that we could also find the third, fourth, fifth, and so on values by increasing the indexing values (i.e. 2 and â€“ 1) in the previous R codes.

## Example 2: Return Second Lowest / Highest Value Using min & max Functions

The following syntax shows how to use the min and max functions to get the second lowest and highest values.

We can find the second lowest values as follows:

min(x[x != min(x)]) # Applying min() function # -3 |

min(x[x != min(x)]) # Applying min() function # -3

And the second highest value as follows:

max(x[x != max(x)]) # Applying max() function # 5 |

max(x[x != max(x)]) # Applying max() function # 5

Using the min and max functions might be more intuitive when searching for low and high values. However, the previous code can only be used to find the **second** lowest and highest values.

## Video & Further Resources

Do you need more explanations on the R programming syntax of this tutorial? Then I can recommend to watch the following video of my YouTube channel. Iâ€™m explaining the R programming codes of this article in the video.

**Please accept YouTube cookies to play this video.** By accepting you will be accessing content from YouTube, a service provided by an external third party.

If you accept this notice, your choice will be saved and the page will refresh.

In addition, I can recommend to read the related R articles of this website. A selection of articles on related topics such as vectors, data elements, and lists can be found below.

- Get Second Sub Entry of Every List Element in R
- Get Frequency of Elements with Certain Value in Vector (2 R Examples)
- Get Last Value of Vector in R
- R Programming Language

Also note that we could apply the previous R codes to data frame columns and variables as well.

In summary: You learned on this page how to **find the second lowest and highest numbers** in the R programming language. Let me know in the comments, in case you have any additional comments or questions.

### Statistics Globe Newsletter

## 2 Comments. Leave new

Hi,

I have a question regarding calculating the second highest and lowest value of a large data base. I want to make a function that calculates automatically the second highest/lowest value of a data base.

Do you have an idea how?

Hi Floor,

Just to clarify your question: You want to know the second highest/lowest value of the entire data frame (not only of a single column), correct?

Regards

Joachim