Sorting in r: sort, order & rank R Functions

[This article was first published on Data Science Tutorials, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post Sorting in r: sort, order & rank R Functions appeared first on
Data Science Tutorials –

Sorting in r, The methods sort(), order(), and rank() in R are used to sort data in this article ().

Sorting in r

The tutorial uses six examples to demonstrate how to use the various sorting functions in the R programming language.

Example 1: sort vs. order vs. rank (Basic Application)

Let’s start by making an example vector in R that we can use in the examples that follow:

x <- c(5, - 5, 3, 0)                             
x                                                  
5 -5  3  0

The four numeric values 4, -10, 8, and 0 are included in our example vector.

Customer Segmentation K Means Cluster

We can now use sort

sort(x)                                          
-5  0  3  5
order(x)                                           
2 4 3 1

Given our sample vector, apply the rank function:

rank(x)                                               
[1] 4 1 3 2

The three functions have the same fundamental R syntax. The result of each function, however, is distinct.

Sorting definition () The sort function in R returns data in ascending or descending order. As seen above, the lowest value (-5) of our sample vector was returned first, followed by the highest value (i.e. 5).

Order is defined as () The order function in R returns the ascending or descending position of each element in its input. Within our example vector, the lowest value (i.e. -5) is at position two, while the maximum value (i.e. 5) is at position 1.

R function rank() definition: The rank function returns the input’s ranking position. The first input element (i.e. 4) is the fourth-lowest, while the second element (i.e. – 5) is the lowest value in our sample vector, as shown in above.

Example 2: Order Data Frame Rows by Column Vector

The second example demonstrates how to sort a data frame’s rows by one of its columns. Let’s start by making a simple data frame in R.

data <- data.frame(x,y = LETTERS[1:4])
data
   x y
1  5 A
2 -5 B
3  3 C
4  0 D

There are two columns in our example data: The x-column is the same as the vector that we used in Example 1. The letters A, B, C, and D appear in the y-column.

We can now sort our data frame by the x-column using the order R function.

data[order(data$x), ]                          
   x y
2 -5 B
4  0 D
3  3 C
1  5 A

The rows of our sample data matrix were sorted from the lowest to the highest value of the x-column, as you can see from the preceding RStudio output.

Example 3: Sort List by Name

You may also use the order function to arrange lists by the name of each list element. Consider the following list as an example.

list <- list(B = 1:5, C = 16:20, A = LETTERS[25:29])
list
$B
[1] 1 2 3 4 5
$C
[1] 16 17 18 19 20
$A
[1] "Y" "Z" NA  NA  NA

There are three list elements in our sample list. B, C, and A are the names of the list elements.

We can now arrange our list alphabetically using the order command.

list[order(names(list))]                       
$A
[1] "Y" "Z" NA  NA  NA
$B
[1] 1 2 3 4 5
$C
[1] 16 17 18 19 20

List element A comes first, B comes second, and C comes third after applying the prior R syntax.

Example 4: Sort & Order Descendingly

We’ve sorted our data in ascending order so far. The sort and order functions, on the other hand, are also used to sort in descending order.

Consider the R code for the sort function below…

sort(x, decreasing = TRUE)                           
5  3  0 -5

For the order function, use the R code below.

order(x, decreasing = TRUE)
[1] 1 3 4 2

Both functions, as you can see, delivered the same results to the RStudio console as Example 1, but in the opposite direction.

Example 5: Sort Data Table by Group

We can sort our data in R using multiple columns or vectors (i.e. by complex groups). To begin, let’s make a data table for the following example:

data <- data.frame(value = c(16, 22, 14, 21, 18, 15), 
group1 = c("B", "A", "B", "C", "A", "B"),
group2 = c("E", "E", "E", "D", "D", "D"))
data
    value group1 group2
1    16      B      E
2    22      A      E
3    14      B      E
4    21      C      D
5    18      A      D
6    15      B      D

There are three columns in our example data. We wish to organize some numeric data in the first column by the group. Our groupings are represented by the letters in the second and third columns.

With the order function, we can sort our example data as follows.

data[order(data$group1,data$group2), ]  
   value group1 group2
5    18      A      D
2    22      A      E
6    15      B      D
1    16      B      E
3    14      B      E
4    21      C      D

As you can see, we simply used the order function to insert both grouping columns, resulting in an output data frame that was sorted first by the first grouping column (group1) and then by the second grouping column (group2) (i.e. group2).

Example 6: Lesser-Known Sorting

Sort, order, and rank are by far the most commonly used R functions for sorting data. However, there are a few lesser-known R sorting routines that could be beneficial in certain situations.

You can use the is. unsorted function to see if a vector or column is sorted. If the input is unsorted, the function returns TRUE; if the input is sorted, the function returns FALSE:

is.unsorted(x)
TRUE

The sort.list method is similar to the order function, except that instead of accepting several arguments, it only accepts one.

Best Books to Learn R Programming – Data Science Tutorials

sort.list(x)                                         
2 4 3 1

The sort.int function is identical to the sort function, except it has more options for definition. For further information, look up sort.int in the R help (type?sort.int into the RStudio console).

sort.int(x)
-5  0  3  5

The xtfrm function returns a numeric vector with the same sorting order as the input:

xtfrm(x)                                             
5 -5  3  0

Learning Tips!

The post Sorting in r: sort, order & rank R Functions appeared first on Data Science Tutorials

To leave a comment for the author, please follow the link and comment on their blog: Data Science Tutorials.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)