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

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

blog

The 4 Best Data Analytics Bootcamps in 2024

Discover the best data analytics bootcamps in 2024, discussing what they are, how to choose the best bootcamp, and you can learn.

Kevin Babitz

5 min

blog

A Guide to Corporate Data Analytics Training

Understand the importance of corporate data analytics training in driving business success. Learn about key building blocks and steps to launch an effective training initiative tailored to your organization's needs.

Kevin Babitz

6 min

podcast

[Radar Recap] Scaling Data ROI: Driving Analytics Adoption Within Your Organization with Laura Gent Felker, Omar Khawaja and Tiffany Perkins-Munn

Laura, Omar and Tiffany explore best practices when it comes to scaling analytics adoption within the wider organization

Richie Cotton

40 min

tutorial

Sorting Data in R

How to sort a data frame in R.
DataCamp Team's photo

DataCamp Team

2 min

tutorial

How to Transpose a Matrix in R: A Quick Tutorial

Learn three methods to transpose a matrix in R in this quick tutorial
Adel Nehme's photo

Adel Nehme

code-along

Getting Started With Data Analysis in Alteryx Cloud

In this session, you'll learn how to get started with the Alteryx AI Platform by performing data analysis using Alteryx Designer Cloud.
Joshua Burkhow's photo

Joshua Burkhow

See MoreSee More