sort in r: How to Sort Data in R

Sorting is a common operation in Programming is when data is arranged into meaningful order that makes it simpler to understand and analyze. In R, you can store data in various data types such as vectors, data frames, matrices, and arrays.

sort in r

The sort() is a built-in R function used to sort or order a vector or factor (partially) into ascending or descending order. The sort() function takes an input which can be either a vector or factor, and the second parameter decreasing. If the value of decreasing is TRUE, then the output will be in descending order; otherwise, it will be in ascending order.

Syntax

sort(x, decreasing = FALSE, na.last = NA, …)

Parameters

The is for sorting an R object with a class or a numeric, logical, character, or complex vector. 

The decreasing parameter is logical, which can be TRUE or FALSE.

The na.last argument is for controlling the treatment of NAs. If it is TRUE, then missing values in the data are put last; if its value is FALSE, they are put first; if NA, they are removed.

How to sort vectors in R

To sort vectors in R, use the sort() method. R Vector is a sequence of data items of the same data type.

rv <- c(19, 21, 46, 11)
cat("The vector is: ", rv, "\n")
cat("The sorting vector is: ", sort(rv, decreasing = FALSE))

Output

The vector is: 19 21 46 11
The sorting vector is: 11 19 21 46

From the output, you can see that the sorting is in ascending order.

We can sort in descending order bypassing decreasing = TRUE.

rv <- c(19, 21, 46, 11)
cat("The vector is: ", rv, "\n")
cat("The sorting vector is: ", sort(rv, decreasing = TRUE))

Output

The vector is: 19 21 46 11
The sorting vector is: 46 21 19 11

By default, R will sort the vector in ascending order, but if you pass the decreasing value to TRUE, it will sort in descending order.

How to Sort Matrix in R

To sort matrix in R, use the order() method. A matrix data type is similar to a data frame in R, except that all columns in a matrix must be of the same data type, which can be either numeric or character.

Let’s create a 3 x 3 matrix first using matrix() function.

v <- c(11, 21, 13, 41, 15, 61, 17, 81, 19)
mtrx <- matrix(v, nrow = 3, ncol = 3)
print(mtrx)

Output

    [,1] [,2] [,3]
[1,] 11   41   17
[2,] 21   15   81
[3,] 13   61   19

To sort the matrix by the second column in ascending order, we would use the order() function. The order()function returns a permutation that rearranges its first argument into ascending or descending order.

v <- c(11, 21, 13, 41, 15, 61, 17, 81, 19)
mtrx <- matrix(v, nrow = 3, ncol = 3)
print(mtrx)

cat("After sorting a matrix: ", "\n")
print(mtrx[order(mtrx[, 2]),])

Output

     [,1] [,2] [,3]
[1,]  11   41   17
[2,]  21   15   81
[3,]  13   61   19
After sorting a matrix:
     [,1] [,2] [,3]
[1,]  21   15   81
[2,]  11   41   17
[3,]  13   61   19

You can see that the second column is sorted in ascending order. Therefore, if you want to sort the first or third column, you must pass the column index to get your result.

How to Sort Data Frame in R

To sort a data frame in R, use the order() function. Data Frame is a table or two-dimensional array-like structure in which a row holds a set of values, and each column contains values of one variable.

Consider the following data frame.

df <- data.frame(
 service_id = c(1:5),
 service_name = c("Netflix", "Disney+", "HBOMAX", "Hulu", "Peacock"),
 service_price = c(18, 10, 15, 7, 12),
 stringsAsFactors = FALSE
)
# Print the data frame.
print(df)

Output

   service_id  service_name  service_price
1     1          Netflix        18
2     2          Disney+        10
3     3          HBOMAX         15
4     4          Hulu            7
5     5          Peacock        12

To sort the data frame in descending order by service_price, apply the order() function with the column to sort by specified in the function.

df <- data.frame(
 service_id = c(1:5),
 service_name = c("Netflix", "Disney+", "HBOMAX", "Hulu", "Peacock"),
 service_price = c(18, 10, 15, 7, 12),
 stringsAsFactors = FALSE
)
# Print the data frame.
print(df)

cat("After sorting a dataframe: ", "\n")
df[order(-df$service_price),]

Output

   service_id   service_name   service_price
1     1          Netflix            18
2     2          Disney+            10
3     3          HBOMAX             15
4     4          Hulu                7
5     5          Peacock            12
After sorting a dataframe:
   service_id   service_name  service_price
1     1           Netflix          18
3     3           HBOMAX           15
5     5           Peacock          12
2     2           Disney+          10
4     4           Hulu              7

The negative sign (-) in front of the column name (df$service_price) is applied to execute the sort in descending order. 

Here, we have passed the column name as a reference, but we can also give the column index to the order() function, which will get the exact output.

df <- data.frame(
 service_id = c(1:5),
 service_name = c("Netflix", "Disney+", "HBOMAX", "Hulu", "Peacock"),
 service_price = c(18, 10, 15, 7, 12),
 stringsAsFactors = FALSE
)
# Print the data frame.
print(df)

cat("After sorting a dataframe: ", "\n")
df[order(-df[, 3]),]

That’s pretty much it for sorting data frame in R.

For most applications, you should use the function of the order function instead of using the sort function for sorting the data in a frame. However, in the case of this example, it is recommended to save the newly sorted data in the form of a new data frame to do numerous sorting exercises without having to rearrange the data frame.

Conclusion

Sorting is the most common operation in any programming language. In this article, we have seen how to sort the vector, matrix, and data frame in ascending or descending order.

Leave a Comment