Sorting data frames

Here we use data from the British Election Study. The data set bes2010feelings-pre-long.RData is prepared from the original available at https://www.britishelectionstudy.com/data-object/2010-bes-cross-section/ by removing identifying information and scrambling the data.

load("bes2010feelings-pre-long.RData")

Here we use order()

ii <- with(bes2010flngs_pre_long,order(id,party))
bes2010flngs_pre_long_sorted <- bes2010flngs_pre_long[ii,]
head(bes2010flngs_pre_long_sorted[c("party","id",
                                    "flng.leaders","flng.parties")],n=15)
                      party id flng.leaders flng.parties
1.Conservative Conservative  1            3            6
1.Labour             Labour  1            6            5
1.LibDem             LibDem  1            3            4
1.SNP                   SNP  1           NA           NA
1.Plaid Cymru   Plaid Cymru  1            5           NA
1.Green               Green  1           NA            7
1.UKIP                 UKIP  1           NA            3
1.BNP                   BNP  1           NA            0
2.Conservative Conservative  2            7            6
2.Labour             Labour  2            3            1
2.LibDem             LibDem  2            5            7
2.SNP                   SNP  2           NA           NA
2.Plaid Cymru   Plaid Cymru  2            3           NA
2.Green               Green  2           NA            6
2.UKIP                 UKIP  2           NA            0

Some more convenient altarnatives: Using a Sort() function:

Sort <- function(data,...){
    ii <- eval(substitute(order(...)),
                          envir=data,
                          enclos=parent.frame())
    data[ii,]
}
bes2010flngs_pre_long_sorted <- Sort(bes2010flngs_pre_long,
                                     id,party)

There is a sort() method function provided by the memisc package, which makes sorting a data frame a bit easier. You may need to install this package using install.packages("memisc") from CRAN if you want to run this on your computer. (The package is already installed on the notebook container, however.)

library(memisc)
Loading required package: lattice
Loading required package: MASS

Attaching package: 'memisc'

The following objects are masked from 'package:stats':

    contr.sum, contr.treatment, contrasts

The following object is masked from 'package:base':

    as.array
bes2010flngs_pre_long_sorted <- sort(bes2010flngs_pre_long,
                                     by=~party+id)
head(bes2010flngs_pre_long_sorted[c("party","id",
                                    "flng.leaders","flng.parties")],n=15)
                      party id flng.leaders flng.parties
1.Conservative Conservative  1            3            6
1.Labour             Labour  1            6            5
1.LibDem             LibDem  1            3            4
1.SNP                   SNP  1           NA           NA
1.Plaid Cymru   Plaid Cymru  1            5           NA
1.Green               Green  1           NA            7
1.UKIP                 UKIP  1           NA            3
1.BNP                   BNP  1           NA            0
2.Conservative Conservative  2            7            6
2.Labour             Labour  2            3            1
2.LibDem             LibDem  2            5            7
2.SNP                   SNP  2           NA           NA
2.Plaid Cymru   Plaid Cymru  2            3           NA
2.Green               Green  2           NA            6
2.UKIP                 UKIP  2           NA            0