
Last chance! 50% off unlimited learning
Sale ends in
Abbreviation: srt
Sorts the values of a data frame according to the values of one or more variables contained in the data frame, or the row names. Variable types include numeric and factor variables. Factors are sorted by the ordering of their values, which, by default is alphabetical. Sorting by row names is also possible.
Sort(by, direction=NULL, data=mydata, quiet=getOption("quiet"), ...)srt(…)
One or more variables to be sorted, or just the character string
row.names
or random
.
Default is ascending for all variables listed in by
.
Or, specify a list of "+"
for ascending and "-"
for
descending, one for each variable to be sorted.
The name of the data frame from which to create the subset, which
is mydata
by default.
If set to TRUE
, no text output. Can change system default
with style
function.
Parameter values.
The sorted data frame is returned, usually assigned the name of mydata
as in the examples below. This is the default name for the data frame input into the lessR
data analysis functions.
Sort
sorts the rows of a data frame and lists the first five rows of the sorted data frame. Specify the values upon which to base the sort with the required by
parameter. If not all sorted variables are sorted in ascending order, then also specify a sequence of "+"
for ascending and "-"
for descending, respectively, one for each variable to be sorted. If row.names
or random
is specified, then no other variables can be specified.
A list of consecutive variables can be specified using the colon notation, such as Years:Salary To specify a list of multiple variables, or "+"
and "-"
signs, or sets of variables, separate each set of variables or each sign by a comma, then invoke the R combine or c
function. For example, if three variables are to be sorted, the first two ascending and the last descending, then specify, direction=c("+","+","-")
.
Sort
is based on the standard R function order
, though the Sort
function allows for the sorting of factors, whereas order
does not.
# NOT RUN {
# construct data frame
mydata <- read.table(text="Severity Description
1 Mild
4 Moderate
3 Moderate
2 Mild
1 Severe", header=TRUE)
# sort the data frame called mydata according to Severity
# in ascending order
mydata <- Sort(Severity)
# abbreviated form, replace original with sorted
mydata <- srt(Severity)
# sort Description in descending order, sort Severity within
# each level of Description in ascending order
mydata <- Sort(c(Description, Severity), direction=c("-", "+"))
# data in a different data frame than mydata
data(dataEmployee)
mydata <- Sort(Gender, data=dataEmployee)
# sort by row names in ascending order
mydata <- Read("Employee", format="lessR")
mydata <- Sort(row.names)
# randomly re-shuffle the rows of data
mydata <- Read("Employee", format="lessR")
mydata <- Sort(random)
# }
Run the code above in your browser using DataLab