Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

dfOrder

Sort (order) a dataframe or matrix by multiple columns


Description

Although order will order a vector, and it is possible to order several columns of a data.frame by specifying each column individually in the call to order, dfOrder will order a dataframe or matrix by as many columns as desired.

Usage

dfOrder(object, columns,absolute=FALSE,ascending=TRUE)

Arguments

object

The data.frame to be sorted

columns

Column numbers to use for sorting. If positive, then they will be sorted in increasing order. If negative, then in decreasing order

absolute

If TRUE, then sort the absolute values

ascending

By default, order from smallest to largest.

Details

This is just a simple helper function to reorder data.frames. Originally developed to organize IRT output from the ltm package. It is a basic add on to the order function.

(Completely rewritten for version 1.8.1.)

Value

The original data frame is now in sorted order.

Author(s)

William Revelle

See Also

Other useful file manipulation functions include read.file to read in data from a file or read.clipboard from the clipboard, fileScan, filesList, filesInfo, and fileCreate

dfOrder code is used in the test.irt function to combine ltm and sim.irt output.

Examples

set.seed(42)
x <- matrix(sample(1:4,64,replace=TRUE),ncol=4)
dfOrder(x)  # sort by all columns
dfOrder(x,c(1,4))  #sort by the first and 4th column
x.df <- data.frame(x)
dfOrder(x.df,c(1,-2))  #sort by the first in increasing order, 
   #the second in decreasing order

psychTools

Tools to Accompany the 'psych' Package for Psychological Research

v2.1.12
GPL (>= 2)
Authors
William Revelle [aut, cre] (<https://orcid.org/0000-0003-4880-9610>)
Initial release
2021-12-02

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.