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:
- ConstituencyResults2010.sav
- ConstituencyResults2010.por
- ConstituencyResults2010.dta
- ConstituencyResults2010-stata-new.dta
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
- R file: importing-data-with-haven.R
- Rmarkdown file: importing-data-with-haven.Rmd
- Jupyter notebook file: importing-data-with-haven.ipynb
- Interactive version of the Jupyter notebook (shuts down after 60s):
- Interactive version of the Jupyter notebook (sign in required):