crosstab | R Documentation |
Transform a data set into an n x m table, e.g. to be used in certestats::confusion_matrix()
.
crosstab(
df,
identifier,
compare,
outcome,
positive = "^pos.*",
negative = "^neg.*",
...,
na.rm = TRUE,
ignore_case = TRUE
)
df |
a data.frame |
identifier |
a column name to use as identifier, such as a patient ID or an order ID |
compare |
a column name for the two axes of the table: the labels between the outcomes must be compared |
outcome |
a column name containing the outcome values to compare |
positive |
a regex to match the values in |
negative |
a regex to match the values in |
... |
manual regexes for classes if not using |
na.rm |
a logical to indicate whether empty values must be removed before forming the table |
ignore_case |
a logical to indicate whether the case in the values of |
df <- data.frame(
order_nr = sort(rep(LETTERS[1:20], 2)),
test_type = rep(c("Culture", "PCR"), 20),
result = sample(c("pos", "neg"),
size = 40,
replace = TRUE,
prob = c(0.3, 0.9))
)
head(df)
out <- df |> crosstab(order_nr, test_type, result)
out
df$result <- gsub("pos", "#p", df$result)
df$result <- gsub("neg", "#n", df$result)
head(df)
# gives a warning that pattern matching failed:
df |> crosstab(order_nr, test_type, result)
# define the pattern yourself in such case:
df |> crosstab(order_nr, test_type, result,
positive = "#p",
negative = "#n")
# defining classes manually, can be more than 2:
df |> crosstab(order_nr, test_type, result,
ClassA = "#p", Hello = "#n")
if ("certestats" %in% rownames(utils::installed.packages())) {
certestats::confusion_matrix(out)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.