In this set of exercises, we will work with files from statistical software. The first tasks are about importing data, while the later ones are about labelling and exporting.
.sav
version of the data from the German
General Social Survey - ALLBUS 2021.
haven
package for this. The file should be
stored in the data
folder.
library(haven)
allbus_2021 <- read_spss("./data/allbus_2021/ZA5280_v1-0-0.sav")
Your collaborators ask you to share the data. Unfortunately, they do
not use R
, SPSS
or even Stata
and, hence, asks you to export your data as a SAS file.
sas7bdat
).
haven
package provides a function for writing such
files that is called and works in a similar way as the corresponding
function for importing data in this particular format.
write_sas(allbus_2021, "Allbus_SAS_format.sas7bdat")
Unlike in flat files, such as CSV, the variables now have labels. If you aim to learn about them, you may want to have a look into the appendix for labelled data and then work on the following exercises. For this course, however, this is not necessary.
sjlabelled
package for
this. Remember that you can use [ ] ro subset columns/variables (we only
want to print the labels for the first ten variables).
library(sjlabelled)
get_label(allbus_2021[1:10])
## za_nr doi
## "STUDIENNUMMER" "DIGITAL OBJECT IDENTIFIER"
## version respid
## "RELEASE" "IDENTIFIKATIONSNUMMER DES BEFRAGTEN"
## substudy mode
## "TEILSTUDIE" "ERHEBUNGSMODUS DER ALLBUS-HAUPTBEFRAGUNG"
## splt21 eastwest
## "FRAGEBOGENSPLIT (A, B ODER C)" "ERHEBUNGSGEBIET (WOHNGEBIET): WEST - OST"
## german ep01
## "DEUTSCHE STAATSANGEHOERIGKEIT?" "WIRTSCHAFTSLAGE IN DEUTSCHLAND HEUTE"
Unfortunately, it’s all in German. Imagine you are a political
scientist working on a publication in English, and you are interested in
the variable pa01
(left-right self-placement). So you may
want to consider translating the variable into English.
pa01
from
“LINKS-RECHTS-SELBSTEINSTUFUNG, BEFR.” to “left-right self-placement”.
sjlabelled
for this.
allbus_2021$pa01 <-
set_label(
allbus_2021$pa01,
label = "left-right self-placement"
)
get_label(allbus_2021$pa01)
## [1] "left-right self-placement"