Sorting In R Using Order() Tutorial - DataCamp

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.

Tag » How To Sort In R