R/clean_hiv_status.R

Defines functions clean_hiv_status

Documented in clean_hiv_status

#' @title Clean HIV status data
#' 
#' @description Merge levels of server HIV status variable and 
#' convert output hiv_status variable into factor
#'
#' @param x data frame of HIV treatment status changes
#' @param .drop logical flag to define whether to drop original
#' HIV treatment status variable
#'
#' @return data frame of same length as input data frame
#' @importFrom assertthat assert_that

clean_hiv_status <- function(x, .drop = TRUE) {
  
  # Check args ----
  assertthat::assert_that(is.data.frame(x), 
                          is.logical(.drop))
  
  # New variable merging levels ----
  x$hiv_status <- x$hiv_tx_status
  
  x$hiv_status[x$hiv_tx_status %in% c("HIV 1st line",
                                      "HIV 2nd line",
                                      "HIV 3rd line")] <- "On treatment"
  
  x$hiv_status[x$hiv_tx_status == "Returned to care after previous exit"] <- 
    "Returned to care"
  
  x$hiv_status[x$hiv_tx_status == "Declared lost to follow-up"] <- 
    "LTFU"
  
  # Convert status change to factor ----
  x$hiv_status <-
    factor(x$hiv_status,
           levels = hiv_levels$simple_status_levels)
  
  # Drop original status variable ----
  if (.drop) x$hiv_tx_status <- NULL
  
  x
}
JayAchar/hisreportr documentation built on March 18, 2020, 5:57 a.m.