In this tutorial, we will look at how to sort a vector in descending order in the R programming language with the help of some examples.
How do you sort a vector in R?
You can use the built-in sort()
function to sort a vector in R. It sorts the vector in ascending order by default. Pass decreasing=TRUE
to sort in descending order.
The following is the syntax –
sort(x, decreasing=TRUE, na.last=NA)
It returns the sorted vector.
The sort()
function takes the following arguments –
x
– The object (in our case a vector) to sort.decreasing
(optional) – Whether to sortx
in descending order. It isFALSE
by default.na.last
(optional) – How to treat theNA
values inx
. If it’sTRUE
, theNA
values are put last, if it’sFALSE
, theNA
values are put first and if it’sNA
, theNA
values are removed in the sorted vector. It isNA
by default.
Steps to sort a vector in descending order in R
Let’s now look at a step-by-step example of using the sort()
function to sort a vector in descending order.
Step 1 – Create a vector
First, we will create a vector of some numbers that we will be using throughout this tutorial.
# create a vector vec <- c(4, 6, 2, 1, 3, 7, 5) # display the vector print(vec)
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
[1] 4 6 2 1 3 7 5
Here we created a vector, vec
containing some numbers. You can see that the values in vec
are not sorted.
Step 2 – Sort the vector in descending order
To sort the vector in descending order, pass it to the sort()
function with decreasing = TRUE
.
# sort vector new_vec <- sort(vec, decreasing = TRUE) # display the sorted vector print(new_vec)
Output:
[1] 7 6 5 4 3 2 1
You can see that the resulting vector is sorted in descending order.
Extra – Sort vector with NA
values in descending order
What if we sort a vector containing NA
values in R?
Let’s find out.
# create a vector vec <- c(4, 6, 2, NA, 1, 3, NA, NA, 7, 5) # sort vector new_vec <- sort(vec, decreasing = TRUE) # display the sorted vector print(new_vec)
Output:
[1] 7 6 5 4 3 2 1
Here, we create a vector with some numbers and NA
values and then sort it in descending order using the sort()
function. You can see that, by default, the sort()
function removed the NA
values and returned the sorted vector.
You can change this behavior. For example, if you don’t want to remove the NA
values and want them sorted to the end, pass na.last = TRUE
to the sort()
function.
# create a vector vec <- c(4, 6, 2, NA, 1, 3, NA, NA, 7, 5) # sort vector with NA to end new_vec <- sort(vec, decreasing = TRUE, na.last = TRUE) # display the sorted vector print(new_vec)
Output:
[1] 7 6 5 4 3 2 1 NA NA NA
The NA
values are sorted to the end.
If you want to keep the NA
values at the beginning of the sorted vector, pass na.last = FALSE
.
# create a vector vec <- c(4, 6, 2, NA, 1, 3, NA, NA, 7, 5) # sort vector with NA to start new_vec <- sort(vec, decreasing = TRUE, na.last = FALSE) # display the sorted vector print(new_vec)
Output:
[1] NA NA NA 7 6 5 4 3 2 1
The NA
values are at the beginning of the sorted vector.
Summary – Sort Vector in Descending Order in R
In this tutorial, we looked at how to sort a vector in descending order in R. The following is a short summary of the steps mentioned in this tutorial.
- Create a vector (skip this step if you already have a vector to sort).
- Use the
sort()
function withdecreasing = TRUE
to sort the vector in descending order. It removes theNA
values in the vector by default. To sort theNA
values to the end, passna.last = TRUE
. To sort theNA
values to the start, passna.last = FALSE
.
You might also be interested in –
- Get the Maximum value in an R Vector
- Check if an Element is present in an R Vector
- R – Count Occurrences of a Value in a Vector
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.