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

Downloadable R script and interactive version

Explanation

The link with the “jupyterhub” icon directs you to an interactive Jupyter1 notebook, which runs inside a Docker container2. There are two variants of the interative notebook. One shuts down after 60 seconds and does not require a sign it. The other requires sign in using your ORCID3 credentials, yet shuts down only after 24 hours. (There is no guarantee that such a container persists that long, it may be shut down earlier for maintenance purposes.) After shutdown all data within the container will be reset, i.e. all files created by the user will be deleted.4

Above you see a rendered version of the Jupyter notebook.5

1

For more information about Jupyter see http://jupyter.org. The Jupyter notebooks make use of the RKernel package.

2

For more information about Docker see https://docs.docker.com/. The container images are run with docker spawner.

3

ORCID is a free service for the authentication of researchers. It also allows to showcase publications and contributions to the academic community such as peer review.. See https://info.orcid.org/what-is-orcid/ for more information.

4

The Jupyter notebooks come with NO WARRANTY whatsoever. They are provided for educational and illustrative purposes only. Do not use them for production work.

5

The notebook is rendered with the help of the nbsphinx extension.