1
0
mirror of https://github.com/msberends/AMR.git synced 2025-07-10 08:22:04 +02:00

documentation for 'data.table' AB selectors

This commit is contained in:
2023-03-11 16:54:02 +01:00
parent 45e840c02f
commit 7ad8635994
9 changed files with 174 additions and 40 deletions

View File

@ -118,10 +118,12 @@ not_intrinsic_resistant(
(internally) a \link{character} vector of column names, with additional class \code{"ab_selector"}
}
\description{
These functions allow for filtering rows and selecting columns based on antibiotic test results that are of a specific antibiotic class or group, without the need to define the columns or antibiotic abbreviations. In short, if you have a column name that resembles an antimicrobial drug, it will be picked up by any of these functions that matches its pharmaceutical class: "cefazolin", "CZO" and "J01DB04" will all be picked up by \code{\link[=cephalosporins]{cephalosporins()}}.
These functions allow for filtering rows and selecting columns based on antibiotic test results that are of a specific antibiotic class or group (according to the \link{antibiotics} data set), without the need to define the columns or antibiotic abbreviations.
In short, if you have a column name that resembles an antimicrobial drug, it will be picked up by any of these functions that matches its pharmaceutical class: "cefazolin", "kefzol", "CZO" and "J01DB04" will all be picked up by \code{\link[=cephalosporins]{cephalosporins()}}.
}
\details{
These functions can be used in data set calls for selecting columns and filtering rows. They are heavily inspired by the \link[tidyselect:language]{Tidyverse selection helpers} such as \code{\link[tidyselect:everything]{everything()}}, but also work in base \R and not only in \code{dplyr} verbs. Nonetheless, they are very convenient to use with \code{dplyr} functions such as \code{\link[dplyr:select]{select()}}, \code{\link[dplyr:filter]{filter()}} and \code{\link[dplyr:summarise]{summarise()}}, see \emph{Examples}.
These functions can be used in data set calls for selecting columns and filtering rows. They work with base \R, the Tidyverse, and \code{data.table}. They are heavily inspired by the \link[tidyselect:language]{Tidyverse selection helpers} such as \code{\link[tidyselect:everything]{everything()}}, but are not limited to \code{dplyr} verbs. Nonetheless, they are very convenient to use with \code{dplyr} functions such as \code{\link[dplyr:select]{select()}}, \code{\link[dplyr:filter]{filter()}} and \code{\link[dplyr:summarise]{summarise()}}, see \emph{Examples}.
All columns in the data in which these functions are called will be searched for known antibiotic names, abbreviations, brand names, and codes (ATC, EARS-Net, WHO, etc.) according to the \link{antibiotics} data set. This means that a selector such as \code{\link[=aminoglycosides]{aminoglycosides()}} will pick up column names like 'gen', 'genta', 'J01GB03', 'tobra', 'Tobracin', etc.
@ -174,6 +176,10 @@ All data sets in this \code{AMR} package (about microorganisms, antibiotics, SIR
# See ?example_isolates.
example_isolates
# Examples sections below are split into 'base R', 'dplyr', and 'data.table':
# base R ------------------------------------------------------------------
# select columns 'IPM' (imipenem) and 'MEM' (meropenem)
@ -196,7 +202,7 @@ example_isolates[all(carbapenems()), ]
# filter with multiple antibiotic selectors using c()
example_isolates[all(c(carbapenems(), aminoglycosides()) == "R"), ]
# filter + select in one go: get penicillins in carbapenems-resistant strains
# filter + select in one go: get penicillins in carbapenem-resistant strains
example_isolates[any(carbapenems() == "R"), penicillins()]
# You can combine selectors with '&' to be more specific. For example,
@ -206,13 +212,19 @@ example_isolates[any(carbapenems() == "R"), penicillins()]
# and erythromycin is not a penicillin:
example_isolates[, penicillins() & administrable_per_os()]
# ab_selector() applies a filter in the `antibiotics` data set and is thus very
# flexible. For instance, to select antibiotic columns with an oral DDD of at
# least 1 gram:
# ab_selector() applies a filter in the `antibiotics` data set and is thus
# very flexible. For instance, to select antibiotic columns with an oral DDD
# of at least 1 gram:
example_isolates[, ab_selector(oral_ddd > 1 & oral_units == "g")]
# dplyr -------------------------------------------------------------------
\donttest{
# dplyr -------------------------------------------------------------------
if (require("dplyr")) {
tibble(kefzol = random_sir(5)) \%>\%
select(cephalosporins())
}
if (require("dplyr")) {
# get AMR for all aminoglycosides e.g., per ward:
example_isolates \%>\%
@ -293,5 +305,34 @@ if (require("dplyr")) {
z <- example_isolates \%>\% filter(if_all(carbapenems(), ~ .x == "R"))
identical(x, y) && identical(y, z)
}
# data.table --------------------------------------------------------------
# data.table is supported as well, just use it in the same way as with
# base R, but add `with = FALSE` if using a single AB selector:
if (require("data.table")) {
dt <- as.data.table(example_isolates)
print(
dt[, carbapenems()] # incorrect, returns column *names*
)
print(
dt[, carbapenems(), with = FALSE] # so `with = FALSE` is required
)
# for multiple selections or AB selectors, `with = FALSE` is not needed:
print(
dt[, c("mo", aminoglycosides())]
)
print(
dt[, c(carbapenems(), aminoglycosides())]
)
# row filters are also supported:
print(dt[any(carbapenems() == "S"), ])
print(dt[any(carbapenems() == "S"), penicillins(), with = FALSE])
}
}
}

View File

@ -29,7 +29,8 @@ sir_confidence_interval(
as_percent = FALSE,
only_all_tested = FALSE,
confidence_level = 0.95,
side = "both"
side = "both",
collapse = FALSE
)
proportion_R(..., minimum = 30, as_percent = FALSE, only_all_tested = FALSE)
@ -77,6 +78,8 @@ sir_df(
\item{side}{the side of the confidence interval to return. The default is \code{"both"} for a length 2 vector, but can also be (abbreviated as) \code{"min"}/\code{"left"}/\code{"lower"}/\code{"less"} or \code{"max"}/\code{"right"}/\code{"higher"}/\code{"greater"}.}
\item{collapse}{a \link{logical} to indicate whether the output values should be 'collapsed', i.e. be merged together into one value, or a character value to use for collapsing}
\item{data}{a \link{data.frame} containing columns with class \code{\link{sir}} (see \code{\link[=as.sir]{as.sir()}})}
\item{translate_ab}{a column name of the \link{antibiotics} data set to translate the antibiotic abbreviations to, using \code{\link[=ab_property]{ab_property()}}}
@ -172,6 +175,10 @@ sir_confidence_interval(example_isolates$AMX)
sir_confidence_interval(example_isolates$AMX,
confidence_level = 0.975
)
sir_confidence_interval(example_isolates$AMX,
confidence_level = 0.975,
collapse = ", "
)
# determines \%S+I:
susceptibility(example_isolates$AMX)