Importing data with the haven package

The following makes use of the haven package. You may need to install it from CRAN using the code install.packages("haven") if you want to run this on your computer. (The package is already installed on the notebook container, however.)

This notebook makes use of the following files:

Currently, these Data files are also available from https://www.pippanorris.com/data. (Previously they were available from http://www.hks.harvard.edu/fs/pnorris/Data/Data.htm.)


library(haven)

# Reading in an SPSS "system" file
ConstRes2010 <- read_sav("ConstituencyResults2010.sav")
head(ConstRes2010)
  refno cons lab  libdem snp  plcym green bnp ukip
1 1     14.3 51.9 16.3     NA  7.1  NA    4.1 1.6
2 2     35.8 24.5 19.3     NA 17.8  NA     NA 2.1
3 3     12.4 44.4 18.6   22.2   NA  NA    1.7  NA
4 4     20.7 36.5 28.4   11.9   NA   1    1.2  NA
5 5     30.3 13.6 38.4   15.7   NA  NA    1.1 0.9
6 6      8.7 58.2  8.1   23.6   NA  NA     NA  NA

# Reading in an SPSS "portable" file
ConstRes2010 <- read_por("ConstituencyResults2010.por") # This works with 'foreign', but not with 'haven'
head(ConstRes2010)
Read bytes: 0   String: ⁸/  Ending state: 0
Error parsing double string (length=4): ⁸/ [8/]
Error in df_parse_por_file(spec, encoding = "", user_na = user_na, cols_skip, : Failed to parse ConstituencyResults2010.por: Invalid file, or file has unsupported features.
Traceback:

1. read_por("ConstituencyResults2010.por")
2. df_parse_por_file(spec, encoding = "", user_na = user_na, cols_skip,
 .     n_max, skip, name_repair = .name_repair)

# Reading in a Stata file
ConstRes2010 <- read_dta("ConstituencyResults2010.dta")
head(ConstRes2010)
  refno cons lab  libdem snp  plcym green bnp ukip
1 1     14.3 51.9 16.3     NA  7.1  NA    4.1 1.6
2 2     35.8 24.5 19.3     NA 17.8  NA     NA 2.1
3 3     12.4 44.4 18.6   22.2   NA  NA    1.7  NA
4 4     20.7 36.5 28.4   11.9   NA   1    1.2  NA
5 5     30.3 13.6 38.4   15.7   NA  NA    1.1 0.9
6 6      8.7 58.2  8.1   23.6   NA  NA     NA  NA

# Reading in a new format Stata file
ConstRes2010 <- read_dta("ConstResults2010-stata-new.dta")
head(ConstRes2010)
  refno cons lab  libdem snp  plcym green bnp ukip
1 1     14.3 51.9 16.3     NA  7.1  NA    4.1 1.6
2 2     35.8 24.5 19.3     NA 17.8  NA     NA 2.1
3 3     12.4 44.4 18.6   22.2   NA  NA    1.7  NA
4 4     20.7 36.5 28.4   11.9   NA   1    1.2  NA
5 5     30.3 13.6 38.4   15.7   NA  NA    1.1 0.9
6 6      8.7 58.2  8.1   23.6   NA  NA     NA  NA

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 IRKernel package.

2

For more information about Docker see https://docs.docker.com/. The container images were created with repo2docker, while containers 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.