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 Functions in R

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

FunctionDescriptionComment
append()Add elements to a vectorn/a
class()Return or change the class attribute of an objectn/a
cut()Creates a category object by dividing continuous data into intervalsGenerates cuts at specific points or for a specific number of equal width intervals
duplicated()Returns a logical value for input values without any repititionsInverse is x[!duplicated(x)]
intersect()Returns values shared by two objectsn/a
is.element(x,y)Logical tests if the elements of x are in yVector of TRUE/FALSE results
length()Number of elements in a vectorn/a
match(), unmatch()Compares an object to a table and returns 1 or na to identify matchesn/a
order()Returns the vector of permutations that will sort the input values in ascending ordern/a
replace()Replace elements in a vectorn/a
rev()Returns input vector in reverse ordern/a
rle()Computes the length of runs of the same valuen/a
setdiff()Determines elements of x not in yIgnores elements of y not in x
setequal()Logical test if the elements of two objects are equalReturns only one TRUE or FALSE
sort()Sort in ascending ordern/a
sort.col()Sorts values of a column(s)One or two dimensional data objects only
union()Combines unique values of x and yn/a
unique()Returns input values without any repetitionsn/a
is.unsorted(0Logical test to determine if values are unsortedn/a
rank()Returns the sample ranks of the values in a vector. Ties (i.e., equal values) and missing values can be handled in several ways.n/a
which()Give the locations or TRUE indices for a logical test The array equivalent is arrayInd()
which.min()
which.max()
Give the location or indices of the true min and maxn/a

Back | Next

Leave a Reply

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