R/attach_smoke.R

Defines functions attach_smoke

Documented in attach_smoke

#' smoke
#'
#' @param data data
#' @param years years
#' @param Year logical
#'
#' @return
#' return three class of smoke
#' - never: smoked less than 100 cigarettes in life
#' - former: smoked less than 100 cigarettes in life and smoke not at all now
#' - now: smoked moth than 100 cigarettes in life and smoke some days or every day
#' @export
#'
attach_smoke <- function(data,years,Year=FALSE){
    if (!missing(data)) years <- unique(data$Year)
    years <- prepare_years(years)
    smq <- nhs_tsv('smq\\.|smq_',cat = FALSE,years=years)
    d <- nhs_read(smq,'smq020:smoke','smq040:smoke_now',lower_cd = TRUE,cat = FALSE)
    d <- to_NA(d)
    d$smoke[d$smoke=='no'] <- 'never'
    d$smoke[d$smoke=='yes'] <- 'now'
    d$smoke[grepl('not {0,}at {0,}all',d$smoke_now)] <- 'former'
    d <- d[,c('Year',"seqn","smoke")]
    if (!Year) d <- drop_col(d,'Year')
    if (missing(data)) return(d)
    dplyr::left_join(data,d,'seqn')
}
yikeshu0611/nhanesR documentation built on Jan. 29, 2022, 6:08 a.m.