How to Sort a data.table in R (With Example)


Often you may want to sort the rows of a data.table in R based on one or more columns.

You can use the following methods to do so:

Method 1: Sort Rows in Ascending Order Based on Values in One Column

dt[order(team)]

This particular example sorts the rows of the data.table named dt in ascending order based on the values in the team column.

Method 2: Sort Rows in Descending Order Based on Values in One Column

dt[order(-team)]

This particular example sorts the rows of the data.table named dt in descending order based on the values in the team column.

Method 3: Sort Rows Based on Multiple Columns

dt[order(-points, assists)]

This particular example sorts the rows of the data.table named dt first by the values in the points column in descending order, then by the values in the assists column in ascending order.

The following examples show how to use each method in practice with the following data.table in R:

library(data.table)

#create data table
dt <- data.table(team=c('A', 'A', 'C', 'A', 'B', 'C', 'B', 'B'),
                 points=c(99, 99, 86, 88, 95, 74, 78, 93),
                 assists=c(22, 28, 31, 35, 34, 45, 28, 31),
                 rebounds=c(30, 28, 24, 24, 30, 36, 30, 29))

#view data table
dt

   team points assists rebounds
1:    A     99      22       30
2:    A     99      28       28
3:    C     86      31       24
4:    A     88      35       24
5:    B     95      34       30
6:    C     74      45       36
7:    B     78      28       30
8:    B     93      31       29

Example 1: Sort Rows in Ascending Order Based on Values in One Column

We can use the following syntax to sort the rows of the data.table named dt in ascending order based on the values in the team column.:

#sort rows based on values in team column in ascending order
dt[order(team)] 

   team points assists rebounds
1:    A     99      22       30
2:    A     99      28       28
3:    A     88      35       24
4:    B     95      34       30
5:    B     78      28       30
6:    B     93      31       29
7:    C     86      31       24
8:    C     74      45       36

Notice that the rows are sorted in ascending order based on the values in the team column.

Note: Since the team column is a character column, the order function sorts the rows in alphabetical order from A to Z.

Example 2: Sort Rows in Ascending Order Based on Values in One Column

We can use the following syntax to sort the rows of the data.table named dt in descending order based on the values in the team column.:

#sort rows based on values in team column in descending order
dt[order(-team)] 

   team points assists rebounds
1:    C     86      31       24
2:    C     74      45       36
3:    B     95      34       30
4:    B     78      28       30
5:    B     93      31       29
6:    A     99      22       30
7:    A     99      28       28
8:    A     88      35       24

Notice that the rows are sorted in descending order based on the values in the team column.

Note: Since the team column is a character column, the order function sorts the rows in reverse alphabetical order from Z to A.

Example 3: Sort Rows Based on Multiple Columns

We can use the following syntax to sort the rows of the data.table named dt in descending order by points, then in ascending order by assists:

#sort rows based on values in points descending, then by assists ascending
dt[order(-points, assists)] 

   team points assists rebounds
1:    A     99      22       30
2:    A     99      28       28
3:    B     95      34       30
4:    B     93      31       29
5:    A     88      35       24
6:    C     86      31       24
7:    B     78      28       30
8:    C     74      45       36

Notice that the rows are sorted in descending order based on the values in the points column, then in ascending order by the values in the assists column.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Filter a Vector in R
How to Filter a data.table in R
How to Remove Rows with Any Zeros in R
How to Remove Empty Rows from Data Frame in R

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *