Data Sorting in R

Data sorting in R is simple and straightforward.  Key functions include sort() and order().   The variable by which sort you can be a numeric, a string or a factor variable.  Argument options also provide flexibility how missing values will be handled:  they can be listed first, last or removed.

Data Sorting Examples

It is also possible to sort in reverse order by using a minus sign ( – ) in front of the sort variable.  For example:

Data Sorting with Missing Values

The order() function has the na.last = argument, which allows missing values to be sorted first, last or removed.  Setting the argument to TRUE, FALSE or NA is outline below.  First, missing values are assigned to the data vector used previously.

Data Sorting with subset()

A data subset is a portion of a data object that is sorted to meet logical conditions, as shown below.

Data Sorting Functions in R

Other commonly used functions for sorting or manipulating data sets appear in the table below:

[table id=8 /]

Back | Next