How can I sort a data frame by a specific column in R, and what are some examples of how to do so?

Sorting a data frame is a common task in data analysis and can be easily done in R using the built-in function “order()”. This function allows the user to sort the rows of a data frame based on a specific column, either in ascending or descending order. To use this function, the user needs to specify the data frame and the column to be sorted. For example, to sort a data frame named “df” by the column “Age” in ascending order, the code would be: “df[order(df$Age),]”. This will arrange the rows of the data frame in ascending order, based on the values in the “Age” column. Similarly, to sort in descending order, the code would be: “df[order(-df$Age),]”. Other examples of sorting a data frame by a specific column can include sorting by alphabetical order, numeric values, or dates. Overall, the “order()” function in R makes sorting data frames by a specific column a simple and efficient task.

Sort a Data Frame by Column in R (With Examples)


The easiest way to sort a data frame by a column in R is to use the order() function:

#sort ascending
df[order(df$var1), ]

#sort descending
df[order(-df$var1), ]

This tutorial provides several examples of how to use this function in practice with the following data frame:

#create data frame
df <- data.frame(var1=c(1, 3, 3, 4, 5),
                 var2=c(7, 7, 8, 3, 2),
                 var3=letters[1:5])

#view data frame
df

  var1 var2 var3
1    1    7    a
2    3    7    b
3    3    8    c
4    4    3    d
5    5    2    e

Example 1: Sort by One Column

The following code shows how to sort the data frame by the var1 column, both in an ascending and descending manner:

#sort by var1 ascending
df[order(df$var1), ]

  var1 var2 var3
1    1    7    a
2    3    7    b
3    3    8    c
4    4    3    d
5    5    2    e

#sort by var1 descending
df[order(-df$var1), ]

  var1 var2 var3
5    5    2    e
4    4    3    d
2    3    7    b
3    3    8    c
1    1    7    a

Note that we can also sort by a character vector alphabetically:

#sort by var3 ascending
df[order(df$var3), ]

  var1 var2 var3
1    1    7    a
2    3    7    b
3    3    8    c
4    4    3    d
5    5    2    e

Example 2: Sort by Multiple Columns

The following code shows how to sort the data frame by multiple columns:

#sort by var2 ascending, then var1 ascending
df[order(df$var2, df$var1), ]

  var1 var2 var3
5    5    2    e
4    4    3    d
1    1    7    a
2    3    7    b
3    3    8    c

#sort by var2 ascending, then var1 descending
df[order(df$var2, -df$var1), ]

  var1 var2 var3
5    5    2    e
4    4    3    d
2    3    7    b
1    1    7    a
3    3    8    c

Additional Resources

x