Data preparation

Libary the packages

library(tidyverse) 
library(haven) 
library(janitor)
library(ggplot2) 
library(skimr)
library(splitstackshape)

Import the six waves of data

wave1 <- read_dta("anchor1_50percent_Eng.dta")
wave2 <- read_dta("anchor2_50percent_Eng.dta")
wave3 <- read_dta("anchor3_50percent_Eng.dta")
wave4 <- read_dta("anchor4_50percent_Eng.dta")
wave5 <- read_dta("anchor5_50percent_Eng.dta")
wave6 <- read_dta("anchor6_50percent_Eng.dta")

Clean the data

clean_fun <- function(df) {  df %>% 
  transmute(
    id, 
    age, 
    wave,
    sex=as_factor(sex_gen), #make sex_gen as a factor
    relstat=as_factor(relstat), #make relstat as a factor
    relstat=case_when(relstat== "-7 Incomplete data" ~ as.character(NA), #specify when is missing 
                      TRUE ~ as.character(relstat))%>% as_factor(), #make relstat as a factor again
    health=case_when(hlt1<0 ~ as.numeric(NA),  #specify when hlt1 is missing 
                   TRUE ~ as.numeric(hlt1)),
    sat=case_when(sat6<0 ~ as.numeric(NA), #specify when sat6 is missing
                   TRUE ~ as.numeric(sat6)),
    partner=case_when(relstat %in% c("1 Never married single",
                                     "6 Divorced/separated single",
                                     "9 Widowed single") ~ "No",
                    # when relstat has any of the situations, I assign "No"
                    relstat %in% c("2 Never married LAT",
                                   "3 Never married COHAB",
                                   "4 Married COHAB",
                                   "5 Married noncohabiting",
                                   "7 Divorced/separated LAT",
                                   "8 Divorced/separated COHAB",
                                   "10 Widowed LAT", 
                                   "11 Widowed COHAB") ~ 'Yes') %>% as_factor()
                    # when relstat has any of the situations, I assign "Yes"
  )%>% drop_na()  }
wave1a <- clean_fun(wave1)
wave2a <- clean_fun(wave2)
wave3a <- clean_fun(wave3)
wave4a <- clean_fun(wave4)
wave5a <- clean_fun(wave5)
wave6a <- clean_fun(wave6)