sort vector in descending order in R

R – Sort a Vector in Descending Order

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?

sort vector in descending order 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 sort x in descending order. It is FALSE by default.
  • na.last (optional) – How to treat the NA values in x. If it’s TRUE, the NA values are put last, if it’s FALSE, the NA values are put first and if it’s NA, the NA values are removed in the sorted vector. It is NA 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:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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.

  1. Create a vector (skip this step if you already have a vector to sort).
  2. Use the sort() function with decreasing = TRUE to sort the vector in descending order. It removes the NA values in the vector by default. To sort the NA values to the end, pass na.last = TRUE. To sort the NA values to the start, pass na.last = FALSE.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top