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.1.0     v purrr   0.2.5
## v tibble  1.4.2     v dplyr   0.7.7
## v tidyr   0.8.2     v stringr 1.3.1
## v readr   1.1.1     v forcats 0.3.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)