AMR/R/freq.R

68 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 #
2019-01-02 23:24:07 +01:00
# (c) 2019 Berends MS (m.s.berends@umcg.nl), Luz CF (c.f.luz@umcg.nl) #
# #
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. #
# #
# This R package was created for academic research and 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. #
# ==================================================================== #
2019-08-25 22:53:22 +02:00
#' @importFrom clean freq
#' @export
clean::freq
2019-07-29 13:33:48 +02:00
#' @exportMethod freq.mo
2018-07-01 21:40:37 +02:00
#' @importFrom dplyr n_distinct
2019-08-25 22:53:22 +02:00
#' @importFrom clean 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)
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 = "."),
2019-09-22 17:19:59 +02:00
" (", percent(sum(grams == "Gram-negative", na.rm = TRUE) / length(grams), force_zero = TRUE, round = 2),
")"),
2019-08-30 14:50:56 +02:00
`Gram-positive` = paste0(format(sum(grams == "Gram-positive", na.rm = TRUE),
big.mark = ",",
decimal.mark = "."),
2019-09-22 17:19:59 +02:00
" (", percent(sum(grams == "Gram-positive", na.rm = TRUE) / length(grams), force_zero = TRUE, round = 2),
")"),
`Unique genera` = n_distinct(mo_genus(x_noNA, language = NULL)),
`Unique 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
2019-08-25 22:53:22 +02:00
#' @importFrom clean 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)
ab <- suppressMessages(suppressWarnings(AMR::as.ab(x_name)))
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),
`%SI` = AMR::portion_SI(x, minimum = 0, as_percent = TRUE)))
} else {
2019-08-25 22:53:22 +02:00
freq.default(x = x, ...,
.add_header = list(`%SI` = AMR::portion_SI(x, minimum = 0, as_percent = TRUE)))
}
}