gx.sort.df: Function to Multi-Column Sort a Data Frame

Description Usage Arguments Details Author(s) See Also Examples

Description

Function to sort a data frame on any combination of numerical values or factors in any combination of ascending or descending orders. If the function is run as temp <- gx.sort.df(formula, dfname) the sorted data are not displayed, but retained in temp for subsequent use or display.

Usage

1

Arguments

formula

a ‘formula’ defining the variables to be used in the sort and whether the sort for each is to be in ascending or descending order. The sort order is from left to right in the formula. See Details and Examples below.

dfname

the name of the data frame to be sorted.

Details

The sort is controlled by a text string in the form of a ‘formula’, so ~var1+var2 will sort in ascending order of var1, and then within equal values for var1 in ascending order of var2. A preceding + or - before a column name indicates a sort in ascending or descending order, respectively.

The function also works if formula and dfname are reversed in the function call.

Author(s)

Kevin Wright with some ideas from Andy Liaw
Shared on S-News and R-help in September 2004.

See Also

gx.sort

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
## Make test data available
data(kola.c)
attach(kola.c)
names(kola.c)

## Create a small test data set for ID (1), COUNTRY (4),
## As (17), Co (21), Cu (23) and Ni (28)
test<-kola.c[1:25, c(1,4,17,21,23,28)]

## Sort test data into ascending order on the value of Ni
gx.sort.df(~Ni, test)
temp <- gx.sort.df(test, ~Ni)
temp

## Sort test data by Country and descending order of As
gx.sort.df(test, ~COUNTRY-As)

## Sort test data by Country and descending order of both
## As and Ni
gx.sort.df(test, ~COUNTRY-As-Ni)

## Clean-up and detach test data
rm(test)
rm(temp)
detach(kola.c)

rgr documentation built on May 2, 2019, 6:09 a.m.

Related to gx.sort.df in rgr...