Skip to main content
HomeTutorialsR Programming

Sorting in R using order() Tutorial

In this tutorial, you'll learn about sorting using order(). We will cover how to sort vectors using different parameters, sorting dataframes, and more.
Updated Aug 2020  · 6 min read

R provides a different way to sort the data either in ascending or descending order; Data-analysts, and Data scientists use order(), sort() and packages like dplyr to sort data depending upon the structure of the obtained data.

order() can sort vector, matrix, and also a dataframe can be sorted in ascending and descending order with its help, which is shown in the final section of this tutorial.

Syntax of order()

The syntax of order() is shown below:

order(x, decreasing = TRUE or FALSE, na.last = TRUE or FLASE, method = c("auto", "shell", "quick", "radix"))

The argument above in order() states that:

  • x: data-frames, matrices, or vectors
  • decreasing: boolean value; TRUE then sort in descending order or FALSE then sort in ascending order.
  • na.last: boolean value; TRUE then NA indices are put at last or FLASE THEN NA indices are put first.
  • method: sorting method to be used.

order() in R

Let's look at an example of order() in action.

Below the code contains variable x, which includes a vector with a list of numbers. The numbers are ordered according to its index by using order(x).

y = c(4,12,6,7,2,9,5)
order(y)

The above code gives the following output:

5 1 7 3 4 6 2

Here the order() will sort the given numbers according to its index in the ascending order. Since number 2 is the smallest, which has an index as five and number 4 is index 1, and similarly, the process moves forward in the same pattern.

y = c(4,12,6,7,2,9,5)
y[order(y)]

The above code gives the following output:

2 4 5 6 7 9 12

Here the indexing of order is done where the actual values are printed in the ascending order. The values are ordered according to the index using order() then after each value accessed using y[some-value].

Sorting vector using different parameters in order()

Let's look at an example where the datasets contain the value as symbol NA(Not available).

order(x,na.last=TRUE)

x <- c(8,2,4,1,-4,NA,46,8,9,5,3)
order(x,na.last = TRUE)

The above code gives the following output:

5 4 2 11 3 10 1 8 9 7 6

Here the order() will also sort the given list of numbers according to its index in the ascending order. Since NA is present, its index will be placed last, where 6 will be placed last because of na.last=TRUE.

order(x,na.last=FALSE)

order(x,na.last=FALSE)

The above code gives the following output:

6 5 4 2 11 3 10 1 8 9 7

Here the order() will also sort the given list of numbers according to its index in the ascending order. Since NA is present, it's index, which is 6, will be placed first because of na.last=FALSE.

order(x,decreasing=TRUE,na.last=TRUE)

order(x,decreasing=TRUE,na.last=TRUE)

The above code gives the following output:

7 9 1 8 10 3 11 2 4 5 6

Here order() will sort a given list of numbers according to its index in the descending order because of decreasing=TRUE: 46. The largest is placed at index 7, and the other values are arranged in a decreasing manner. Since NA is present, index 6 will be placed last because of na.last=TRUE.

order(x,decreasing=FALSE,na.last=FALSE)

order(x,decreasing=FALSE,na.last=FALSE)

The above code gives the following output:

6 5 4 2 11 3 10 1 8 9 7

Here NA is present which index is 6 will be placed at first because of na.last=FALSE. order() will sort a given list of numbers according to its index in the ascending order because of decreasing=FALSE: -4, which is smallest placed at index 5, and the other values are arranged increasingly.

Start Learning R For Free

Introduction to R

BeginnerSkill Level
4 hr
2.7M learners
Master the basics of data analysis in R, including vectors, lists, and data frames, and practice R with real data sets.

Intermediate R

BeginnerSkill Level
6 hr
590K learners
Continue your journey to becoming an R ninja by learning about conditional statements, loops, and vector functions.

Sorting a dataframe by using order()

Let's create a dataframe where the population value is 10. The variable gender consists of vector values 'male' and 'female' where 10 sample values could be obtained with the help of sample(), whereas replace = TRUE will generate only the unique values. Similarly, the age consists of value from 25 to 75, along with a degree of possible value as c("MA," "ME," "BE," "BSCS"), which again will generate unique values.

Task: To sort the given data in the ascending order based on the given population's age.

Note: The sample data shown may differ while you're trying to use it in your local machine because each time running a code will create a unique dataframe.

population = 10
gender=sample(c("male","female"),population,replace=TRUE)
age = sample(25:75, population, replace=TRUE)
degree = sample(c("MA","ME","BE","BSCS"), population, replace=TRUE)
(final.data = data.frame(gender=gender, age=age, degree=degree))
gender age degree
male 40 MA
female 57 BSCS
male 66 BE
female 61 BSCS
female 48 MA
male 25 MA
female 49 BE
male 52 ME
female 57 MA
female 35 MA

The above code gives the following output, which shows a newly created dataframe.

  gender       age        degree
   male           40         MA
   female       57         BSCS
   male           66         BE
   female       61         BSCS
   female       48        MA
   male           25         MA
   female       49         BE
   male        52        ME
   female       57         MA
   female       35         MA

Let's sort the dataframe in the ascending order by using order() based on the variable age.

order(final.data$age)

The above code gives the following output:

6 10 3 9 5 8 4 2 7 1

Since age 25 is at index 6 followed by age 35 at index 10 and similarly, all the age-related values are arranged in ascending order.

The code below contains the [] order with variable age, is used to arrange in ascending order where the gender, along with degree information is also printed.

final.data[order(final.data$age),]
  gender age degree
6 male 25 MA
10 female 35 MA
1 male 40 MA
5 female 48 MA
7 female 49 BE
8 male 52 ME
2 female 57 BSCS
9 female 57 MA
4 female 61 BSCS
3 male 66 BE

The above code gives the following output:

gender    age    degree
6    male    25    MA
10    female    35    MA
1    male    40    MA
5    female    48    MA
7    female    49    BE
8    male    52    ME
2    female    57    BSCS
9    female    57    MA
4    female    61    BSCS
3    male    66    BE

The output above shows that age is arranged in ascending order along with its corresponding gender and degree information is obtained.

Congratulations

Congratulations, you have made it to the end of this tutorial!

You've learned about sorting using order() with its argument with the examples and sorting vector using different parameters and final example, which contains sorting of the dataframe.

To know more detail about this topic, you can visit Order function in R

If you would like to learn more about R, take DataCamp's Introduction to R course.

Topics

R Courses

Certification available

Course

Introduction to R

4 hr
2.7M
Master the basics of data analysis in R, including vectors, lists, and data frames, and practice R with real data sets.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

Data Sets and Where to Find Them: Navigating the Landscape of Information

Are you struggling to find interesting data sets to analyze? Do you have a plan for what to do with a sample data set once you’ve found it? If you have data set questions, this tutorial is for you! We’ll go over the basics of what a data set is, where to find one, how to clean and explore it, and where to showcase your data story.

Amberle McKee

11 min

You’re invited! Join us for Radar: The Analytics Edition

Join us for a full day of events sharing best practices from thought leaders in the analytics space
DataCamp Team's photo

DataCamp Team

4 min

10 Top Data Analytics Conferences for 2024

Discover the most popular analytics conferences and events scheduled for 2024.
Javier Canales Luna's photo

Javier Canales Luna

7 min

A Complete Guide to Alteryx Certifications

Advance your career with our Alteryx certification guide. Learn key strategies, tips, and resources to excel in data science.
Matt Crabtree's photo

Matt Crabtree

9 min

Scaling Enterprise Analytics with Libby Duane Adams, Chief Advocacy Officer and Co-Founder of Alteryx

RIchie and Libby explore the differences between analytics and business intelligence, generative AI and its implications in analytics, the role of data quality and governance, Alteryx’s AI platform, data skills as a workplace necessity, and more. 
Richie Cotton's photo

Richie Cotton

43 min

Mastering Bayesian Optimization in Data Science

Unlock the power of Bayesian Optimization for hyperparameter tuning in Machine Learning. Master theoretical foundations and practical applications with Python to enhance model accuracy.
Zoumana Keita 's photo

Zoumana Keita

11 min

See MoreSee More