Learn R Programming

rgr (version 1.1.0)

gx.sort: Function to Single Column Sort a Matrix or Dataframe

Description

Function to sort a matrix or dataframe by the value in a column. On exit the function displays the sorted data. Any NAs in the sort column are sorted to beyond the greatest value. If the function is run as temp <- gx.sort(x, ncol) the sorted data are not displayed, but retained in temp for subsequent use or display.

Usage

gx.sort(x, col = 1, reverse = FALSE)

Arguments

x
the matrix or dataframe to be sorted.
col
a column number, the value of which will be used to sort the matrix or dataframe
reverse
the default is to sort in ascending order of the value in column col. If a descending order sort is required, set reverse = TRUE.

See Also

gx.sort.df

Examples

Run this code
## Make test data available
data(sind)
attach(sind)

## Sort dataframe sind into ascending order on the value
## of column 4, Zn
gx.sort(sind, 4)

## Sort dataframe sind into descending order on the value
## of column 4, Zn
gx.sort(sind, 4, reverse = TRUE)

## Sort only the geochemical data in dataframe sind into
## descending order on the value of column 4, Zn.  Note
## that sind[, -c(1:3)] moves the old column 4 to
## position 1
gx.sort(sind[, -c(1:3)], 1, reverse = TRUE)

## Detach test data
detach(sind)

Run the code above in your browser using DataLab