1
0
mirror of https://github.com/msberends/AMR.git synced 2025-07-08 20:41:58 +02:00

improve as.mo()

This commit is contained in:
2023-05-24 15:55:53 +02:00
parent 3018fb87a9
commit 0bcf55d3b6
14 changed files with 76 additions and 22 deletions

View File

@ -69,7 +69,7 @@
#' ab_atc_group2("AMX")
#' ab_url("AMX")
#'
#' # smart lowercase tranformation
#' # smart lowercase transformation
#' ab_name(x = c("AMC", "PLB"))
#' ab_name(x = c("AMC", "PLB"), tolower = TRUE)
#'

View File

@ -61,7 +61,7 @@
#' av_group("ACI")
#' av_url("ACI")
#'
#' # smart lowercase tranformation
#' # lowercase transformation
#' av_name(x = c("ACI", "VALA"))
#' av_name(x = c("ACI", "VALA"), tolower = TRUE)
#'

View File

@ -191,13 +191,14 @@ first_isolate <- function(x = NULL,
}
meet_criteria(col_specimen, allow_class = "character", has_length = 1, allow_NULL = TRUE, is_in = colnames(x))
if (is.logical(col_icu)) {
meet_criteria(col_icu, allow_class = "logical", has_length = c(1, nrow(x)), allow_NA = TRUE)
meet_criteria(col_icu, allow_class = "logical", has_length = c(1, nrow(x)), allow_NA = TRUE, allow_NULL = TRUE)
x$newvar_is_icu <- col_icu
} else if (!is.null(col_icu)) {
# add "logical" to the allowed classes here, since it may give an error in certain user input, and should then also say that logicals can be used too
meet_criteria(col_icu, allow_class = c("character", "logical"), has_length = 1, allow_NULL = TRUE, is_in = colnames(x))
x$newvar_is_icu <- x[, col_icu, drop = TRUE]
} else {
x$newvar_is_icu <- NA_real_
x$newvar_is_icu <- NA
}
# method
method <- coerce_method(method)

11
R/mo.R
View File

@ -281,9 +281,16 @@ as.mo <- function(x,
x_parts <- strsplit(gsub("-", " ", x_out, fixed = TRUE), " ", fixed = TRUE)[[1]]
# do a pre-match on first character (and if it contains a space, first chars of first two terms)
if (length(x_parts) %in% c(2, 3)) {
if (length(x_parts) == 1) {
# for genus or species or subspecies
filtr <- which(AMR_env$MO_lookup$full_first == substr(x_parts, 1, 1) |
AMR_env$MO_lookup$species_first == substr(x_parts, 1, 1) |
AMR_env$MO_lookup$subspecies_first == substr(x_parts, 1, 1))
} else if (length(x_parts) %in% c(2, 3)) {
# for genus + species + subspecies
filtr <- which(AMR_env$MO_lookup$full_first == substr(x_parts[1], 1, 1) & (AMR_env$MO_lookup$species_first == substr(x_parts[2], 1, 1) | AMR_env$MO_lookup$subspecies_first == substr(x_parts[2], 1, 1)))
filtr <- which(AMR_env$MO_lookup$full_first == substr(x_parts[1], 1, 1) &
(AMR_env$MO_lookup$species_first == substr(x_parts[2], 1, 1) |
AMR_env$MO_lookup$subspecies_first == substr(x_parts[2], 1, 1)))
} else if (length(x_parts) > 3) {
first_chars <- paste0("(^| )", "[", paste(substr(x_parts, 1, 1), collapse = ""), "]")
filtr <- which(AMR_env$MO_lookup$full_first %like_case% first_chars)

View File

@ -759,7 +759,7 @@ as_sir_method <- function(method_short,
if (is.null(mo)) {
stop_("No information was supplied about the microorganisms (missing argument `mo` and no column of class 'mo' found). See ?as.sir.\n\n",
"To transform certain columns with e.g. mutate(), use `data %>% mutate(across(..., as.sir, mo = x))`, where x is your column with microorganisms.\n",
"To tranform all ", method_long, " in a data set, use `data %>% as.sir()` or `data %>% mutate_if(is.", method_short, ", as.sir)`.",
"To transform all ", method_long, " in a data set, use `data %>% as.sir()` or `data %>% mutate_if(is.", method_short, ", as.sir)`.",
call = FALSE
)
}