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 by as many columns as desired.
dfOrder(object, columns)
| 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 |
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.
The original data frame is now in sorted order.
This code is used in the test.irt function to combine ltm and sim.irt output.
x <- data.frame(matrix(sample(1:4,64,replace=TRUE),ncol=4)) dfOrder(x) # sort by all columns#> X1 X2 X3 X4 #> 12 1 3 2 1 #> 1 1 4 2 3 #> 15 1 4 4 1 #> 3 2 3 1 3 #> 7 2 3 1 3 #> 13 2 3 2 1 #> 5 2 3 2 4 #> 16 2 4 2 4 #> 2 3 1 3 3 #> 9 3 1 4 4 #> 10 3 3 1 4 #> 4 3 3 2 4 #> 8 3 4 4 4 #> 6 4 3 1 4 #> 11 4 4 2 3 #> 14 4 4 4 2dfOrder(x,c(1,4)) #sort by the first and 4th column#> X1 X2 X3 X4 #> 12 1 3 2 1 #> 15 1 4 4 1 #> 1 1 4 2 3 #> 13 2 3 2 1 #> 3 2 3 1 3 #> 7 2 3 1 3 #> 5 2 3 2 4 #> 16 2 4 2 4 #> 2 3 1 3 3 #> 4 3 3 2 4 #> 8 3 4 4 4 #> 9 3 1 4 4 #> 10 3 3 1 4 #> 14 4 4 4 2 #> 11 4 4 2 3 #> 6 4 3 1 4dfOrder(x,c(1,-2)) #sort by the first in increaseing order,#> X1 X2 X3 X4 #> 12 1 -3 2 -1 #> 1 1 -4 2 -3 #> 15 1 -4 4 -1 #> 3 2 -3 1 -3 #> 5 2 -3 2 -4 #> 7 2 -3 1 -3 #> 13 2 -3 2 -1 #> 16 2 -4 2 -4 #> 2 3 -1 3 -3 #> 9 3 -1 4 -4 #> 4 3 -3 2 -4 #> 10 3 -3 1 -4 #> 8 3 -4 4 -4 #> 6 4 -3 1 -4 #> 11 4 -4 2 -3 #> 14 4 -4 4 -2#the second in decreasing order