R/aps_read_2019.R

Defines functions aps_read_2019

Documented in aps_read_2019

#' Read APS 2019
#'
#' Reads and performs basic cleaning on the Annual Population Survey 2019. From this year, there
#' are no variables capturing smoking behaviour or happiness/life satisfaction.
#'
#' @param root Character - the root directory
#' @param file Character - the file path and name
#'
#'
#' @return Returns a data table
#' @export
aps_read_2019 <- function(
  root = c("C:/"),
  file = "Users/User/Documents/Datasets/Annual Population Survey/raw data/apsp_jan_dec19_eul_pwta18.tab"
) {

  data <- data.table::fread(
    paste0(root[1], file),
    na.strings = c("NA", "", "-1", "-2", "-6", "-7", "-8", "-9", "-90", "-90.0", "N/A")
  )

  setnames(data, names(data), tolower(names(data)))

  ### keep relevant variables

  weights_vars     <- Hmisc::Cs(pwta18,piwta18)
  demographic_vars <- Hmisc::Cs(age,sex,govtof,ethukeul)
  family_vars      <- Hmisc::Cs(marsta)
  education_vars   <- Hmisc::Cs(hiqul15d)
  work_vars        <- Hmisc::Cs(inecac05,grsswk,ftptwk,ttachr,ttushr,illwk,illoff)
  #health_vars      <- Hmisc::Cs(happy,satis,cigsmk16)
  other_vars       <- Hmisc::Cs(refwkm)

  names <- c(demographic_vars,family_vars,education_vars,work_vars,       weights_vars,other_vars)
  names <- tolower(names)

  data <- data[ ,names, with=F]

  ### tidy data

  # rename of variables which change over time.

  data <- rename(data,
                 c("hiqual" = "hiqul15d"),
                 c("month" = "refwkm"),
                   c("pwt" = "pwta18"),
                   c("piwt" = "piwta18")
  )

  # date variables

  data$quarter <- recode(as.character(data$month),
                         "1" = "1" ,
                         "2" = "1" ,
                         "3" = "1" ,
                         "4" = "2" ,
                         "5" = "2" ,
                         "6" = "2" ,
                         "7" = "3" ,
                         "8" = "3" ,
                         "9" = "3" ,
                         "10" = "4" ,
                         "11" = "4" ,
                         "12" = "4" )

  data$year <- 2019

  return(data[])
}
djmorris1989/apsclean documentation built on June 17, 2020, 9:02 p.m.