1
0
mirror of https://github.com/msberends/AMR.git synced 2024-12-27 11:26:11 +01:00
AMR/R/freq.R

73 lines
3.8 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-10-04 15:36:12 +02:00
#' @importFrom clean freq.default
# @importFrom clean percentage
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-09-23 13:53:50 +02:00
`Nr of genera` = n_distinct(mo_genus(x_noNA, language = NULL)),
`Nr 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
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)))
}
}