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
)a column name to use as identifier, such as a patient ID or an order ID
a column name for the two axes of the table: the labels between the outcomes must be compared
a column name containing the outcome values to compare
a stringr::regex to match the values in outcome that must be considered as the Positive class, use FALSE to not use a Positive class
a stringr::regex to match the values in outcome that must be considered as the Negative class, use FALSE to not use a Negative class
manual stringr::regexes for classes if not using positive and negative, such as Class1 = "c1", Class2 = "c2", Class3 = "c3"
a logical to indicate whether empty values must be removed before forming the table
a logical to indicate whether the case in the values of positive, negative and ... must be ignored
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)
#> order_nr test_type result
#> 1 A Culture neg
#> 2 A PCR neg
#> 3 B Culture neg
#> 4 B PCR neg
#> 5 C Culture neg
#> 6 C PCR neg
out <- df |> crosstab(order_nr, test_type, result)
out
#> PCR
#> Culture Positive Negative
#> Positive 1 2
#> Negative 3 14
df$result <- gsub("pos", "#p", df$result)
df$result <- gsub("neg", "#n", df$result)
head(df)
#> order_nr test_type result
#> 1 A Culture #n
#> 2 A PCR #n
#> 3 B Culture #n
#> 4 B PCR #n
#> 5 C Culture #n
#> 6 C PCR #n
# gives a warning that pattern matching failed:
df |> crosstab(order_nr, test_type, result)
#> Warning: Check the regular expressions in the 'positive' and 'negative' arguments - they are not both matched
#> PCR
#> Culture Positive Negative
#> Positive 0 0
#> Negative 0 0
# define the pattern yourself in such case:
df |> crosstab(order_nr, test_type, result,
positive = "#p",
negative = "#n")
#> PCR
#> Culture Positive Negative
#> Positive 1 2
#> Negative 3 14
# defining classes manually, can be more than 2:
df |> crosstab(order_nr, test_type, result,
ClassA = "#p", Hello = "#n")
#> PCR
#> Culture ClassA Hello
#> ClassA 1 2
#> Hello 3 14
if ("certestats" %in% rownames(utils::installed.packages())) {
certestats::confusion_matrix(out)
}
#> Error: Required package(s) 'progress' not installed