AMR/R/freq.R

71 lines
3.7 KiB
R
Raw Normal View History

# ==================================================================== #
# TITLE #
# Antimicrobial Resistance (AMR) Analysis #
# #
2019-01-02 23:24:07 +01:00
# SOURCE #
# https://gitlab.com/msberends/AMR #
# #
# LICENCE #
# (c) 2018-2020 Berends MS, Luz CF et al. #
# #
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. #
# #
# 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. #
2019-04-05 18:47:39 +02:00
# Visit our website for more info: https://msberends.gitlab.io/AMR. #
# ==================================================================== #
#' @importFrom cleaner freq
2019-08-25 22:53:22 +02:00
#' @export
cleaner::freq
2019-08-25 22:53:22 +02:00
2019-07-29 13:33:48 +02:00
#' @exportMethod freq.mo
2020-05-16 13:05:47 +02:00
#' @importFrom cleaner freq.default
2018-07-01 21:40:37 +02:00
#' @export
2019-01-17 12:08:04 +01:00
#' @noRd
2019-07-29 13:33:48 +02:00
freq.mo <- function(x, ...) {
2019-09-22 17:19:59 +02:00
x_noNA <- as.mo(x[!is.na(x)]) # as.mo() to get the newest mo codes
2019-08-30 14:50:56 +02:00
grams <- mo_gramstain(x_noNA, language = NULL)
digits <- list(...)$digits
if (is.null(digits)) {
digits <- 2
}
2019-08-25 22:53:22 +02:00
freq.default(x = x, ...,
2019-08-30 14:50:56 +02:00
.add_header = list(`Gram-negative` = paste0(format(sum(grams == "Gram-negative", na.rm = TRUE),
big.mark = ",",
decimal.mark = "."),
" (", percentage(sum(grams == "Gram-negative", na.rm = TRUE) / length(grams), digits = digits),
2019-09-22 17:19:59 +02:00
")"),
2019-08-30 14:50:56 +02:00
`Gram-positive` = paste0(format(sum(grams == "Gram-positive", na.rm = TRUE),
big.mark = ",",
decimal.mark = "."),
" (", percentage(sum(grams == "Gram-positive", na.rm = TRUE) / length(grams), digits = digits),
2019-09-22 17:19:59 +02:00
")"),
2019-11-29 19:43:23 +01:00
`No of genera` = n_distinct(mo_genus(x_noNA, language = NULL)),
`No of species` = n_distinct(paste(mo_genus(x_noNA, language = NULL),
2019-08-30 14:50:56 +02:00
mo_species(x_noNA, language = NULL)))))
2019-01-17 12:08:04 +01:00
}
2019-07-29 13:33:48 +02:00
#' @exportMethod freq.rsi
#' @importFrom cleaner freq.default
2018-07-09 14:02:58 +02:00
#' @export
2018-07-08 22:14:55 +02:00
#' @noRd
2019-07-29 13:33:48 +02:00
freq.rsi <- function(x, ...) {
x_name <- deparse(substitute(x))
x_name <- gsub(".*[$]", "", x_name)
2020-02-14 19:54:13 +01:00
ab <- suppressMessages(suppressWarnings(as.ab(x_name)))
2019-07-29 13:33:48 +02:00
if (!is.na(ab)) {
2019-08-25 22:53:22 +02:00
freq.default(x = x, ...,
.add_header = list(Drug = paste0(ab_name(ab), " (", ab, ", ", ab_atc(ab), ")"),
group = ab_group(ab),
2020-02-14 19:54:13 +01:00
`%SI` = susceptibility(x, minimum = 0, as_percent = TRUE)))
} else {
2019-08-25 22:53:22 +02:00
freq.default(x = x, ...,
2020-02-14 19:54:13 +01:00
.add_header = list(`%SI` = susceptibility(x, minimum = 0, as_percent = TRUE)))
}
}