This function creates a contingency table by aggregating counts based on a grouping variable.
Usage
create_contingency_table(
data,
group_col,
success_col,
failure_col,
prefix = NULL,
col_names = NULL
)Arguments
- data
A data frame containing the data
- group_col
String name of the column to group by (e.g., "dose")
- success_col
String name of the column containing success counts (e.g., "alive")
- failure_col
String name of the column containing failure counts (e.g., "dead")
- prefix
String prefix to add to row names (default: the group column name followed by "_")
- col_names
Character vector of length 2 for column names (default: c("success", "failure"))
Examples
# Generate example data
set.seed(123)
data <- data.frame(
dose = rep(c(0, 1, 10), each = 3),
alive = c(9, 8, 10, 7, 6, 5, 3, 4, 2),
dead = c(1, 2, 0, 3, 4, 5, 7, 6, 8)
)
# Create contingency table
create_contingency_table(data, "dose", "alive", "dead")
#> success failure
#> dose_0 27 3
#> dose_1 18 12
#> dose_10 9 21