Chapter 2 Create Data Set
2.1 Load packages or install them if not already installed
if(!require("haven")){install.packages("haven"); library(haven)}
## Loading required package: haven
if(!require("tidyverse")){install.packages("tidyverse"); library(tidyverse)}
## Loading required package: tidyverse
## -- Attaching packages ------- tidyverse 1.2.1 --
## v ggplot2 3.2.0 v purrr 0.3.2
## v tibble 2.1.3 v dplyr 0.8.1
## v tidyr 0.8.3 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.4.0
## -- Conflicts ---------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
2.2 Load raw Swedish Citizen Panel data
Load data using the haven package. Select variables of interest, and create new data set in .sav and .csv formats
scp_raw <- read_sav("Data/Studie3_Esaiasson_20180611.sav") %>%
mutate(idnummer = as.numeric(idnummer))
d <- scp_raw %>% select(
Q64, #age
Q63, #gender
S3_1_1,
S3_2_1,
S3_4_1_1,
S3_4_1_2,
S3_5_1,
S3_6_1_1,
S3_6_1_2,
S3_7_1_1,
S3_7_1_2,
S3_8_1_1,
S3_8_1_2,
Studie3sel
)
#Create data file, .csv format
write.csv(d, "Data/Goodloser-exp1.csv")
#Create data file, .sav format
write_sav(d, "Data/Goodloser-exp1.sav", compress = FALSE)