R/add_clre_tech.R

Defines functions add_clre_tech

Documented in add_clre_tech

#' @title add_clre_tech
#'
#' @description Function to add Technology field to CL Reagent complaints.
#'
#' @param df Dataframe of CL Reagent complaints
#' @param cs_name Name of call subject column. Defaults to call_subject.
#' @param analyzer Name of analyzer column. Default to analayzer.
#'
#' @return Returns dataframe with Technology field added.
#' @export
add_clre_tech <- function(df, cs_name = 'call_subject', analyzer = 'analyzer') {
  cs_name <- 'call_subject'
  analyzer <- 'analyzer'

  technologies <- qrc_link(query = 'select * from clre_tech;')

  df <- df[!df[, cs_name] %in% c('***Reset', 'XTAP', 'XTM'), ]

  temp1 <- df %>%
    dplyr::filter_(
      paste0(cs_name, " %in% c('CALBRTR', 'CKMB', 'QCMAT')")
    ) %>%
    dplyr::mutate(technology = NA)

  if (nrow(temp1) > 0) {
    temp1$technology[temp1[, cs_name] == 'CALBRTR' & temp1[, analyzer] %in% c(
      'FS 4600 SYS', '250', 'FS', '5600'
    )] <- 'MicroSlide'

    temp1$technology[temp1[, cs_name] == 'CALBRTR' & temp1[, analyzer] %in% c(
      'ECI', '3600'
    )] <- 'MicroWell'

    temp1$technology[temp1[, cs_name] == 'CKMB' & temp1[, analyzer] %in% c(
      'FS 4600 SYS', '250', 'FS'
    )] <- 'MicroSlide'

    temp1$technology[temp1[, cs_name] == 'CKMB' & temp1[, analyzer] %in% c(
      'ECI', '3600', '5600'
    )] <- 'MicroWell'

    temp1$technology[temp1[, cs_name] == 'QCMAT' & temp1[, analyzer] %in% c(
      'FS 4600 SYS', '250', 'FS', '5600'
    )] <- 'MicroSlide'

    temp1$technology[temp1[, cs_name] == 'QCMAT' & temp1[, analyzer] %in% c(
      'ECI', '3600'
    )] <- 'MicroWell'
  }

  temp2 <- df %>%
    dplyr::filter_(
      paste0('!(', cs_name, " %in% c('CALBRTR', 'CKMB', 'QCMAT'))")
    ) %>%
    dplyr::mutate(technology = NA)

  if (nrow(temp2) > 0) {
    for (cs in unique(temp2[, cs_name])) {
      if (cs %in% unique(technologies$call_subject)) {
        temp2$technology[temp2[, cs_name] == cs] <- technologies$technology[
          technologies$call_subject == cs
          ]
      }
    }

    temp2$technology[is.na(temp2$technology)] <- 'MicroSlide'

  }
  data <- rbind(temp2, temp1)

  return(data)
}
kimjam/qrcutils documentation built on May 20, 2019, 10:21 p.m.