2018-07-17 13:02:05 +02:00
# ==================================================================== #
# TITLE #
2021-02-02 23:57:35 +01:00
# Antimicrobial Resistance (AMR) Data Analysis for R #
2018-07-17 13:02:05 +02:00
# #
2019-01-02 23:24:07 +01:00
# SOURCE #
2020-07-08 14:48:06 +02:00
# https://github.com/msberends/AMR #
2018-07-17 13:02:05 +02:00
# #
# LICENCE #
2020-12-27 00:30:28 +01:00
# (c) 2018-2021 Berends MS, Luz CF et al. #
2020-10-08 11:16:03 +02:00
# Developed at the University of Groningen, the Netherlands, in #
# collaboration with non-profit organisations Certe Medical #
# Diagnostics & Advice, and University Medical Center Groningen. #
2018-07-17 13:02:05 +02:00
# #
2019-01-02 23:24:07 +01:00
# This R package is free software; you can freely use and distribute #
# it for both personal and commercial purposes under the terms of the #
# GNU General Public License version 2.0 (GNU GPL-2), as published by #
# the Free Software Foundation. #
2020-01-05 17:22:09 +01:00
# We created this package for both routine data analysis and academic #
# research and it was publicly released in the hope that it will be #
# useful, but it comes WITHOUT ANY WARRANTY OR LIABILITY. #
2020-10-08 11:16:03 +02:00
# #
# Visit our website for the full manual and a complete tutorial about #
2021-02-02 23:57:35 +01:00
# how to conduct AMR data analysis: https://msberends.github.io/AMR/ #
2018-07-17 13:02:05 +02:00
# ==================================================================== #
2021-01-18 16:57:56 +01:00
#' Key Antibiotics for First (Weighted) Isolates
2018-07-17 13:02:05 +02:00
#'
2021-01-18 16:57:56 +01:00
#' These function can be used to determine first isolates (see [first_isolate()]). Using key antibiotics to determine first isolates is more reliable than without key antibiotics. These selected isolates can then be called first 'weighted' isolates.
#' @inheritSection lifecycle Stable Lifecycle
2021-01-24 14:48:56 +01:00
#' @param x a [data.frame] with antibiotics columns, like `AMX` or `amox`. Can be left blank to determine automatically
2020-10-19 17:09:19 +02:00
#' @param y,z character vectors to compare
2018-07-17 13:02:05 +02:00
#' @inheritParams first_isolate
2020-10-19 17:09:19 +02:00
#' @param universal_1,universal_2,universal_3,universal_4,universal_5,universal_6 column names of **broad-spectrum** antibiotics, case-insensitive. See details for which antibiotics will be used at default (which are guessed with [guess_ab_col()]).
#' @param GramPos_1,GramPos_2,GramPos_3,GramPos_4,GramPos_5,GramPos_6 column names of antibiotics for **Gram-positives**, case-insensitive. See details for which antibiotics will be used at default (which are guessed with [guess_ab_col()]).
#' @param GramNeg_1,GramNeg_2,GramNeg_3,GramNeg_4,GramNeg_5,GramNeg_6 column names of antibiotics for **Gram-negatives**, case-insensitive. See details for which antibiotics will be used at default (which are guessed with [guess_ab_col()]).
#' @param warnings give a warning about missing antibiotic columns (they will be ignored)
2020-12-22 00:51:17 +01:00
#' @param ... other arguments passed on to functions
2020-12-07 16:06:42 +01:00
#' @details
2021-01-24 14:48:56 +01:00
#' The [key_antibiotics()] function is context-aware. This means that then the `x` argument can be left blank, see *Examples*.
2020-12-07 16:06:42 +01:00
#'
#' The function [key_antibiotics()] returns a character vector with 12 antibiotic results for every isolate. These isolates can then be compared using [key_antibiotics_equal()], to check if two isolates have generally the same antibiogram. Missing and invalid values are replaced with a dot (`"."`) by [key_antibiotics()] and ignored by [key_antibiotics_equal()].
2020-05-16 13:05:47 +02:00
#'
#' The [first_isolate()] function only uses this function on the same microbial species from the same patient. Using this, e.g. an MRSA will be included after a susceptible *S. aureus* (MSSA) is found within the same patient episode. Without key antibiotic comparison it would not. See [first_isolate()] for more info.
2018-07-17 13:02:05 +02:00
#'
2019-11-28 22:32:17 +01:00
#' At default, the antibiotics that are used for **Gram-positive bacteria** are:
#' - Amoxicillin
#' - Amoxicillin/clavulanic acid
#' - Cefuroxime
#' - Piperacillin/tazobactam
#' - Ciprofloxacin
#' - Trimethoprim/sulfamethoxazole
#' - Vancomycin
#' - Teicoplanin
#' - Tetracycline
#' - Erythromycin
#' - Oxacillin
#' - Rifampin
2018-07-17 13:02:05 +02:00
#'
2019-11-28 22:32:17 +01:00
#' At default the antibiotics that are used for **Gram-negative bacteria** are:
#' - Amoxicillin
#' - Amoxicillin/clavulanic acid
#' - Cefuroxime
#' - Piperacillin/tazobactam
#' - Ciprofloxacin
#' - Trimethoprim/sulfamethoxazole
#' - Gentamicin
#' - Tobramycin
#' - Colistin
#' - Cefotaxime
#' - Ceftazidime
#' - Meropenem
#'
#' The function [key_antibiotics_equal()] checks the characters returned by [key_antibiotics()] for equality, and returns a [`logical`] vector.
2021-01-18 16:57:56 +01:00
#' @inheritSection first_isolate Key Antibiotics
2018-07-17 13:02:05 +02:00
#' @rdname key_antibiotics
#' @export
2019-11-28 22:32:17 +01:00
#' @seealso [first_isolate()]
2021-01-18 16:57:56 +01:00
#' @inheritSection AMR Read more on Our Website!
2018-07-17 13:02:05 +02:00
#' @examples
2021-01-24 14:48:56 +01:00
#' # `example_isolates` is a data set available in the AMR package.
2019-08-27 16:45:42 +02:00
#' # See ?example_isolates.
2020-09-29 23:35:46 +02:00
#'
2020-12-07 16:06:42 +01:00
#' # output of the `key_antibiotics()` function could be like this:
2018-07-23 10:35:39 +02:00
#' strainA <- "SSSRR.S.R..S"
#' strainB <- "SSSIRSSSRSSS"
#'
2020-12-07 16:06:42 +01:00
#' # those strings can be compared with:
2018-07-23 10:35:39 +02:00
#' key_antibiotics_equal(strainA, strainB)
2018-12-22 22:39:34 +01:00
#' # TRUE, because I is ignored (as well as missing values)
2018-07-23 10:35:39 +02:00
#'
#' key_antibiotics_equal(strainA, strainB, ignore_I = FALSE)
2020-12-07 16:06:42 +01:00
#' # FALSE, because I is not ignored and so the 4th character differs
2020-09-29 23:35:46 +02:00
#'
#' \donttest{
#' if (require("dplyr")) {
#' # set key antibiotics to a new variable
#' my_patients <- example_isolates %>%
2020-12-07 16:06:42 +01:00
#' mutate(keyab = key_antibiotics()) %>% # no need to define `x`
2020-09-29 23:35:46 +02:00
#' mutate(
#' # now calculate first isolates
2020-12-07 16:06:42 +01:00
#' first_regular = first_isolate(col_keyantibiotics = FALSE),
2020-09-29 23:35:46 +02:00
#' # and first WEIGHTED isolates
2020-12-07 16:06:42 +01:00
#' first_weighted = first_isolate(col_keyantibiotics = "keyab")
2020-09-29 23:35:46 +02:00
#' )
#'
2020-12-07 16:06:42 +01:00
#' # Check the difference, in this data set it results in a lot more isolates:
2020-09-29 23:35:46 +02:00
#' sum(my_patients$first_regular, na.rm = TRUE)
#' sum(my_patients$first_weighted, na.rm = TRUE)
#' }
#' }
2021-02-08 14:18:42 +01:00
key_antibiotics <- function ( x = NULL ,
2018-12-22 22:39:34 +01:00
col_mo = NULL ,
2019-06-11 14:18:25 +02:00
universal_1 = guess_ab_col ( x , " amoxicillin" ) ,
universal_2 = guess_ab_col ( x , " amoxicillin/clavulanic acid" ) ,
universal_3 = guess_ab_col ( x , " cefuroxime" ) ,
universal_4 = guess_ab_col ( x , " piperacillin/tazobactam" ) ,
universal_5 = guess_ab_col ( x , " ciprofloxacin" ) ,
universal_6 = guess_ab_col ( x , " trimethoprim/sulfamethoxazole" ) ,
GramPos_1 = guess_ab_col ( x , " vancomycin" ) ,
GramPos_2 = guess_ab_col ( x , " teicoplanin" ) ,
GramPos_3 = guess_ab_col ( x , " tetracycline" ) ,
GramPos_4 = guess_ab_col ( x , " erythromycin" ) ,
GramPos_5 = guess_ab_col ( x , " oxacillin" ) ,
GramPos_6 = guess_ab_col ( x , " rifampin" ) ,
GramNeg_1 = guess_ab_col ( x , " gentamicin" ) ,
GramNeg_2 = guess_ab_col ( x , " tobramycin" ) ,
GramNeg_3 = guess_ab_col ( x , " colistin" ) ,
GramNeg_4 = guess_ab_col ( x , " cefotaxime" ) ,
GramNeg_5 = guess_ab_col ( x , " ceftazidime" ) ,
GramNeg_6 = guess_ab_col ( x , " meropenem" ) ,
2018-08-31 13:36:19 +02:00
warnings = TRUE ,
2018-12-22 22:39:34 +01:00
... ) {
2021-02-09 12:28:15 +01:00
if ( is_null_or_grouped_tbl ( x ) ) {
# when `x` is left blank, auto determine it (get_current_data() also contains dplyr::cur_data_all())
# is also fix for using a grouped df as input (a dot as first argument)
x <- tryCatch ( get_current_data ( arg_name = " x" , call = -2 ) , error = function ( e ) x )
}
meet_criteria ( x , allow_class = " data.frame" ) # also checks dimensions to be >0
2020-10-19 17:09:19 +02:00
meet_criteria ( col_mo , allow_class = " character" , has_length = 1 , allow_NULL = TRUE , allow_NA = TRUE )
meet_criteria ( universal_1 , allow_class = " character" , has_length = 1 , allow_NULL = TRUE , allow_NA = TRUE )
meet_criteria ( universal_2 , allow_class = " character" , has_length = 1 , allow_NULL = TRUE , allow_NA = TRUE )
meet_criteria ( universal_3 , allow_class = " character" , has_length = 1 , allow_NULL = TRUE , allow_NA = TRUE )
meet_criteria ( universal_4 , allow_class = " character" , has_length = 1 , allow_NULL = TRUE , allow_NA = TRUE )
meet_criteria ( universal_5 , allow_class = " character" , has_length = 1 , allow_NULL = TRUE , allow_NA = TRUE )
meet_criteria ( universal_6 , allow_class = " character" , has_length = 1 , allow_NULL = TRUE , allow_NA = TRUE )
meet_criteria ( GramPos_1 , allow_class = " character" , has_length = 1 , allow_NULL = TRUE , allow_NA = TRUE )
meet_criteria ( GramPos_2 , allow_class = " character" , has_length = 1 , allow_NULL = TRUE , allow_NA = TRUE )
meet_criteria ( GramPos_3 , allow_class = " character" , has_length = 1 , allow_NULL = TRUE , allow_NA = TRUE )
meet_criteria ( GramPos_4 , allow_class = " character" , has_length = 1 , allow_NULL = TRUE , allow_NA = TRUE )
meet_criteria ( GramPos_5 , allow_class = " character" , has_length = 1 , allow_NULL = TRUE , allow_NA = TRUE )
meet_criteria ( GramPos_6 , allow_class = " character" , has_length = 1 , allow_NULL = TRUE , allow_NA = TRUE )
meet_criteria ( GramNeg_1 , allow_class = " character" , has_length = 1 , allow_NULL = TRUE , allow_NA = TRUE )
meet_criteria ( GramNeg_2 , allow_class = " character" , has_length = 1 , allow_NULL = TRUE , allow_NA = TRUE )
meet_criteria ( GramNeg_3 , allow_class = " character" , has_length = 1 , allow_NULL = TRUE , allow_NA = TRUE )
meet_criteria ( GramNeg_4 , allow_class = " character" , has_length = 1 , allow_NULL = TRUE , allow_NA = TRUE )
meet_criteria ( GramNeg_5 , allow_class = " character" , has_length = 1 , allow_NULL = TRUE , allow_NA = TRUE )
meet_criteria ( GramNeg_6 , allow_class = " character" , has_length = 1 , allow_NULL = TRUE , allow_NA = TRUE )
meet_criteria ( warnings , allow_class = " logical" , has_length = 1 )
2020-05-16 13:05:47 +02:00
2021-02-08 14:18:42 +01:00
# force regular data.frame, not a tibble or data.table
x <- as.data.frame ( x , stringsAsFactors = FALSE )
2020-05-16 13:05:47 +02:00
dots <- unlist ( list ( ... ) )
if ( length ( dots ) != 0 ) {
2020-12-22 00:51:17 +01:00
# backwards compatibility with old arguments
2021-01-24 14:48:56 +01:00
dots.names <- names ( dots )
2020-05-16 13:05:47 +02:00
if ( " info" %in% dots.names ) {
warnings <- dots [which ( dots.names == " info" ) ]
}
}
2020-07-13 09:17:24 +02:00
2018-12-22 22:39:34 +01:00
# try to find columns based on type
# -- mo
2019-01-15 12:45:24 +01:00
if ( is.null ( col_mo ) ) {
2019-05-23 16:58:59 +02:00
col_mo <- search_type_in_df ( x = x , type = " mo" )
2021-01-15 22:44:52 +01:00
stop_if ( is.null ( col_mo ) , " `col_mo` must be set" )
} else {
stop_ifnot ( col_mo %in% colnames ( x ) , " column '" , col_mo , " ' (`col_mo`) not found" )
2018-08-31 13:36:19 +02:00
}
2020-07-13 09:17:24 +02:00
2018-07-17 13:02:05 +02:00
# check columns
2018-07-19 15:11:23 +02:00
col.list <- c ( universal_1 , universal_2 , universal_3 , universal_4 , universal_5 , universal_6 ,
GramPos_1 , GramPos_2 , GramPos_3 , GramPos_4 , GramPos_5 , GramPos_6 ,
GramNeg_1 , GramNeg_2 , GramNeg_3 , GramNeg_4 , GramNeg_5 , GramNeg_6 )
2020-05-16 13:05:47 +02:00
check_available_columns <- function ( x , col.list , warnings = TRUE ) {
2019-05-10 16:44:59 +02:00
# check columns
col.list <- col.list [ ! is.na ( col.list ) & ! is.null ( col.list ) ]
names ( col.list ) <- col.list
col.list.bak <- col.list
# are they available as upper case or lower case then?
2019-10-11 17:21:02 +02:00
for ( i in seq_len ( length ( col.list ) ) ) {
2019-05-10 16:44:59 +02:00
if ( is.null ( col.list [i ] ) | isTRUE ( is.na ( col.list [i ] ) ) ) {
col.list [i ] <- NA
2019-05-23 16:58:59 +02:00
} else if ( toupper ( col.list [i ] ) %in% colnames ( x ) ) {
2019-05-10 16:44:59 +02:00
col.list [i ] <- toupper ( col.list [i ] )
2019-05-23 16:58:59 +02:00
} else if ( tolower ( col.list [i ] ) %in% colnames ( x ) ) {
2019-05-10 16:44:59 +02:00
col.list [i ] <- tolower ( col.list [i ] )
2019-05-23 16:58:59 +02:00
} else if ( ! col.list [i ] %in% colnames ( x ) ) {
2019-05-10 16:44:59 +02:00
col.list [i ] <- NA
}
}
2019-05-23 16:58:59 +02:00
if ( ! all ( col.list %in% colnames ( x ) ) ) {
2020-05-16 13:05:47 +02:00
if ( warnings == TRUE ) {
2020-11-10 16:35:56 +01:00
warning_ ( " Some columns do not exist and will be ignored: " ,
col.list.bak [ ! ( col.list %in% colnames ( x ) ) ] %pm>% toString ( ) ,
" .\nTHIS MAY STRONGLY INFLUENCE THE OUTCOME." ,
immediate = TRUE ,
call = FALSE )
2019-05-10 16:44:59 +02:00
}
}
col.list
}
2020-07-13 09:17:24 +02:00
2020-05-16 13:05:47 +02:00
col.list <- check_available_columns ( x = x , col.list = col.list , warnings = warnings )
2018-07-19 15:11:23 +02:00
universal_1 <- col.list [universal_1 ]
universal_2 <- col.list [universal_2 ]
universal_3 <- col.list [universal_3 ]
universal_4 <- col.list [universal_4 ]
universal_5 <- col.list [universal_5 ]
universal_6 <- col.list [universal_6 ]
GramPos_1 <- col.list [GramPos_1 ]
GramPos_2 <- col.list [GramPos_2 ]
GramPos_3 <- col.list [GramPos_3 ]
GramPos_4 <- col.list [GramPos_4 ]
GramPos_5 <- col.list [GramPos_5 ]
GramPos_6 <- col.list [GramPos_6 ]
GramNeg_1 <- col.list [GramNeg_1 ]
GramNeg_2 <- col.list [GramNeg_2 ]
GramNeg_3 <- col.list [GramNeg_3 ]
GramNeg_4 <- col.list [GramNeg_4 ]
GramNeg_5 <- col.list [GramNeg_5 ]
GramNeg_6 <- col.list [GramNeg_6 ]
2020-07-13 09:17:24 +02:00
2018-07-19 15:11:23 +02:00
universal <- c ( universal_1 , universal_2 , universal_3 ,
universal_4 , universal_5 , universal_6 )
2020-07-13 09:17:24 +02:00
2019-10-11 17:21:02 +02:00
gram_positive <- c ( universal ,
2020-07-13 09:17:24 +02:00
GramPos_1 , GramPos_2 , GramPos_3 ,
GramPos_4 , GramPos_5 , GramPos_6 )
2019-01-03 23:56:19 +01:00
gram_positive <- gram_positive [ ! is.null ( gram_positive ) ]
2019-05-10 16:44:59 +02:00
gram_positive <- gram_positive [ ! is.na ( gram_positive ) ]
2021-02-02 23:57:35 +01:00
if ( length ( gram_positive ) < 12 & message_not_thrown_before ( " key_antibiotics.grampos" ) ) {
2020-11-10 16:35:56 +01:00
warning_ ( " Only using " , length ( gram_positive ) , " different antibiotics as key antibiotics for Gram-positives. See ?key_antibiotics." , call = FALSE )
2021-02-02 23:57:35 +01:00
remember_thrown_message ( " key_antibiotics.grampos" )
2019-05-10 16:44:59 +02:00
}
2020-07-13 09:17:24 +02:00
2019-10-11 17:21:02 +02:00
gram_negative <- c ( universal ,
2020-07-13 09:17:24 +02:00
GramNeg_1 , GramNeg_2 , GramNeg_3 ,
GramNeg_4 , GramNeg_5 , GramNeg_6 )
2019-01-03 23:56:19 +01:00
gram_negative <- gram_negative [ ! is.null ( gram_negative ) ]
2019-05-10 16:44:59 +02:00
gram_negative <- gram_negative [ ! is.na ( gram_negative ) ]
2021-02-02 23:57:35 +01:00
if ( length ( gram_negative ) < 12 & message_not_thrown_before ( " key_antibiotics.gramneg" ) ) {
2020-11-10 16:35:56 +01:00
warning_ ( " Only using " , length ( gram_negative ) , " different antibiotics as key antibiotics for Gram-negatives. See ?key_antibiotics." , call = FALSE )
2021-02-02 23:57:35 +01:00
remember_thrown_message ( " key_antibiotics.gramneg" )
2019-05-10 16:44:59 +02:00
}
2020-07-13 09:17:24 +02:00
2020-05-16 13:05:47 +02:00
x [ , col_mo ] <- as.mo ( x [ , col_mo , drop = TRUE ] )
x $ gramstain <- mo_gramstain ( x [ , col_mo , drop = TRUE ] , language = NULL )
x $ key_ab <- NA_character_
2020-05-16 20:08:21 +02:00
2018-07-17 13:02:05 +02:00
# Gram +
2020-09-18 16:05:53 +02:00
x $ key_ab <- pm_if_else ( x $ gramstain == " Gram-positive" ,
2020-05-16 13:05:47 +02:00
tryCatch ( apply ( X = x [ , gram_positive ] ,
MARGIN = 1 ,
FUN = function ( x ) paste ( x , collapse = " " ) ) ,
error = function ( e ) paste0 ( rep ( " ." , 12 ) , collapse = " " ) ) ,
x $ key_ab )
2019-09-12 15:08:53 +02:00
2018-07-17 13:02:05 +02:00
# Gram -
2020-09-18 16:05:53 +02:00
x $ key_ab <- pm_if_else ( x $ gramstain == " Gram-negative" ,
2020-05-16 13:05:47 +02:00
tryCatch ( apply ( X = x [ , gram_negative ] ,
MARGIN = 1 ,
FUN = function ( x ) paste ( x , collapse = " " ) ) ,
error = function ( e ) paste0 ( rep ( " ." , 12 ) , collapse = " " ) ) ,
x $ key_ab )
2020-07-13 09:17:24 +02:00
2018-07-17 13:02:05 +02:00
# format
2020-05-16 13:05:47 +02:00
key_abs <- toupper ( gsub ( " [^SIR]" , " ." , gsub ( " (NA|NULL)" , " ." , x $ key_ab ) ) )
2019-09-12 15:08:53 +02:00
2020-09-18 16:05:53 +02:00
if ( pm_n_distinct ( key_abs ) == 1 ) {
2020-11-10 16:35:56 +01:00
warning_ ( " No distinct key antibiotics determined." , call = FALSE )
2019-09-12 15:08:53 +02:00
}
2020-07-13 09:17:24 +02:00
2018-07-17 13:02:05 +02:00
key_abs
2020-07-13 09:17:24 +02:00
2018-07-17 13:02:05 +02:00
}
#' @rdname key_antibiotics
#' @export
2019-05-23 17:35:58 +02:00
key_antibiotics_equal <- function ( y ,
z ,
2018-07-17 13:02:05 +02:00
type = c ( " keyantibiotics" , " points" ) ,
ignore_I = TRUE ,
points_threshold = 2 ,
info = FALSE ) {
2020-10-19 17:09:19 +02:00
meet_criteria ( y , allow_class = " character" )
meet_criteria ( z , allow_class = " character" )
meet_criteria ( type , allow_class = " character" , has_length = c ( 1 , 2 ) )
meet_criteria ( ignore_I , allow_class = " logical" , has_length = 1 )
2021-01-24 14:48:56 +01:00
meet_criteria ( points_threshold , allow_class = c ( " numeric" , " integer" ) , has_length = 1 , is_positive = TRUE , is_finite = TRUE )
2020-10-19 17:09:19 +02:00
meet_criteria ( info , allow_class = " logical" , has_length = 1 )
stop_ifnot ( length ( y ) == length ( z ) , " length of `y` and `z` must be equal" )
2019-05-23 17:35:58 +02:00
# y is active row, z is lag
x <- y
y <- z
2020-07-13 09:17:24 +02:00
2018-07-17 13:02:05 +02:00
type <- type [1 ]
2020-07-13 09:17:24 +02:00
2018-07-25 14:17:04 +02:00
# only show progress bar on points or when at least 5000 isolates
info_needed <- info == TRUE & ( type == " points" | length ( x ) > 5000 )
2020-07-13 09:17:24 +02:00
2018-07-25 14:17:04 +02:00
result <- logical ( length ( x ) )
2020-07-13 09:17:24 +02:00
2018-07-25 14:17:04 +02:00
if ( info_needed == TRUE ) {
2020-09-18 16:05:53 +02:00
p <- progress_ticker ( length ( x ) )
2020-05-16 13:05:47 +02:00
on.exit ( close ( p ) )
2018-07-25 14:17:04 +02:00
}
2020-07-13 09:17:24 +02:00
2019-10-11 17:21:02 +02:00
for ( i in seq_len ( length ( x ) ) ) {
2020-07-13 09:17:24 +02:00
2018-07-25 14:17:04 +02:00
if ( info_needed == TRUE ) {
2020-05-16 13:05:47 +02:00
p $ tick ( )
2018-07-17 19:51:09 +02:00
}
2020-07-13 09:17:24 +02:00
2018-07-25 14:17:04 +02:00
if ( is.na ( x [i ] ) ) {
2019-10-11 17:21:02 +02:00
x [i ] <- " "
2018-07-25 14:17:04 +02:00
}
if ( is.na ( y [i ] ) ) {
2019-10-11 17:21:02 +02:00
y [i ] <- " "
2018-07-17 13:02:05 +02:00
}
2020-07-13 09:17:24 +02:00
2018-07-25 14:17:04 +02:00
if ( x [i ] == y [i ] ) {
2020-07-13 09:17:24 +02:00
2018-07-25 14:17:04 +02:00
result [i ] <- TRUE
2020-07-13 09:17:24 +02:00
2018-07-25 14:17:04 +02:00
} else if ( nchar ( x [i ] ) != nchar ( y [i ] ) ) {
2020-07-13 09:17:24 +02:00
2018-07-25 14:17:04 +02:00
result [i ] <- FALSE
2020-07-13 09:17:24 +02:00
2018-07-25 14:17:04 +02:00
} else {
2020-07-13 09:17:24 +02:00
2018-07-25 14:17:04 +02:00
x_split <- strsplit ( x [i ] , " " ) [ [1 ] ]
y_split <- strsplit ( y [i ] , " " ) [ [1 ] ]
2020-07-13 09:17:24 +02:00
2019-10-11 17:21:02 +02:00
if ( type == " keyantibiotics" ) {
2020-07-13 09:17:24 +02:00
2018-07-25 14:17:04 +02:00
if ( ignore_I == TRUE ) {
x_split [x_split == " I" ] <- " ."
y_split [y_split == " I" ] <- " ."
}
2020-07-13 09:17:24 +02:00
2018-07-25 14:17:04 +02:00
y_split [x_split == " ." ] <- " ."
x_split [y_split == " ." ] <- " ."
2020-07-13 09:17:24 +02:00
2018-07-25 14:17:04 +02:00
result [i ] <- all ( x_split == y_split )
2020-07-13 09:17:24 +02:00
2019-10-11 17:21:02 +02:00
} else if ( type == " points" ) {
2018-07-17 19:51:09 +02:00
# count points for every single character:
# - no change is 0 points
# - I <-> S|R is 0.5 point
# - S|R <-> R|S is 1 point
# use the levels of as.rsi (S = 1, I = 2, R = 3)
2020-07-13 09:17:24 +02:00
2020-09-18 16:05:53 +02:00
suppressWarnings ( x_split <- x_split %pm>% as.rsi ( ) %pm>% as.double ( ) )
suppressWarnings ( y_split <- y_split %pm>% as.rsi ( ) %pm>% as.double ( ) )
2020-07-13 09:17:24 +02:00
2020-09-18 16:05:53 +02:00
points <- ( x_split - y_split ) %pm>% abs ( ) %pm>% sum ( na.rm = TRUE ) / 2
2018-07-25 14:17:04 +02:00
result [i ] <- points >= points_threshold
2020-07-13 09:17:24 +02:00
2018-07-25 14:17:04 +02:00
} else {
2019-10-11 17:21:02 +02:00
stop ( " `" , type , ' ` is not a valid value for type, must be "points" or "keyantibiotics". See ?key_antibiotics' )
2018-07-17 13:02:05 +02:00
}
}
}
2018-07-25 14:17:04 +02:00
if ( info_needed == TRUE ) {
2020-07-01 11:51:26 +02:00
close ( p )
2018-07-25 14:17:04 +02:00
}
result
2018-07-17 13:02:05 +02:00
}