mirror of
https://github.com/msberends/AMR.git
synced 2025-07-08 18:01:50 +02:00
(v1.3.0.9022) mo_matching_score(), poorman update, as.rsi() fix
This commit is contained in:
38
R/mic.R
38
R/mic.R
@ -19,15 +19,15 @@
|
||||
# Visit our website for more info: https://msberends.github.io/AMR. #
|
||||
# ==================================================================== #
|
||||
|
||||
#' Transform input to minimum inhibitory concentrations
|
||||
#' Transform input to minimum inhibitory concentrations (MIC)
|
||||
#'
|
||||
#' This transforms a vector to a new class [`mic`], which is an ordered [`factor`] with valid minimum inhibitory concentrations (MIC) as levels. Invalid MIC values will be translated as `NA` with a warning.
|
||||
#' This transforms a vector to a new class [`mic`], which is an ordered [factor] with valid minimum inhibitory concentrations (MIC) as levels. Invalid MIC values will be translated as `NA` with a warning.
|
||||
#' @inheritSection lifecycle Stable lifecycle
|
||||
#' @rdname as.mic
|
||||
#' @param x vector
|
||||
#' @param na.rm a logical indicating whether missing values should be removed
|
||||
#' @details To interpret MIC values as RSI values, use [as.rsi()] on MIC values. It supports guidelines from EUCAST and CLSI.
|
||||
#' @return Ordered [`factor`] with new class [`mic`]
|
||||
#' @return Ordered [factor] with additional class [`mic`]
|
||||
#' @aliases mic
|
||||
#' @export
|
||||
#' @seealso [as.rsi()]
|
||||
@ -55,7 +55,7 @@ as.mic <- function(x, na.rm = FALSE) {
|
||||
if (is.mic(x)) {
|
||||
x
|
||||
} else {
|
||||
x <- x %>% unlist()
|
||||
x <- x %pm>% unlist()
|
||||
if (na.rm == TRUE) {
|
||||
x <- x[!is.na(x)]
|
||||
}
|
||||
@ -109,13 +109,13 @@ as.mic <- function(x, na.rm = FALSE) {
|
||||
c(t(sapply(ops, function(x) paste0(x, c(10:98)[9:98 %% 2 == TRUE])))),
|
||||
c(t(sapply(ops, function(x) paste0(x, sort(c(2 ^ c(7:10), 80 * c(2:12))))))))
|
||||
|
||||
na_before <- x[is.na(x) | x == ""] %>% length()
|
||||
na_before <- x[is.na(x) | x == ""] %pm>% length()
|
||||
x[!x %in% lvls] <- NA
|
||||
na_after <- x[is.na(x) | x == ""] %>% length()
|
||||
na_after <- x[is.na(x) | x == ""] %pm>% length()
|
||||
|
||||
if (na_before != na_after) {
|
||||
list_missing <- x.bak[is.na(x) & !is.na(x.bak) & x.bak != ""] %>%
|
||||
unique() %>%
|
||||
list_missing <- x.bak[is.na(x) & !is.na(x.bak) & x.bak != ""] %pm>%
|
||||
unique() %pm>%
|
||||
sort()
|
||||
list_missing <- paste0('"', list_missing, '"', collapse = ", ")
|
||||
warning(na_after - na_before, " results truncated (",
|
||||
@ -196,15 +196,15 @@ print.mic <- function(x, ...) {
|
||||
#' @noRd
|
||||
summary.mic <- function(object, ...) {
|
||||
x <- object
|
||||
n_total <- x %>% length()
|
||||
n_total <- length(x)
|
||||
x <- x[!is.na(x)]
|
||||
n <- x %>% length()
|
||||
c(
|
||||
"Class" = "mic",
|
||||
"<NA>" = n_total - n,
|
||||
"Min." = sort(x)[1] %>% as.character(),
|
||||
"Max." = sort(x)[n] %>% as.character()
|
||||
)
|
||||
n <- length(x)
|
||||
value <- c("Class" = "mic",
|
||||
"<NA>" = n_total - n,
|
||||
"Min." = as.character(sort(x)[1]),
|
||||
"Max." = as.character(sort(x)[n]))
|
||||
class(value) <- c("summaryDefault", "table")
|
||||
value
|
||||
}
|
||||
|
||||
#' @method plot mic
|
||||
@ -283,7 +283,7 @@ barplot.mic <- function(height,
|
||||
#' @export
|
||||
#' @noRd
|
||||
c.mic <- function(x, ...) {
|
||||
y <- NextMethod()
|
||||
attributes(y) <- attributes(x)
|
||||
y
|
||||
y <- unlist(lapply(list(...), as.character))
|
||||
x <- as.character(x)
|
||||
as.mic(c(x, y))
|
||||
}
|
||||
|
Reference in New Issue
Block a user