#' Create 2 by k count table
#'
#' This function takes in a data frame where the first column is a vector of 2 categories, while the second column is a vector of redundant k cases.
#'
#' @param df A data frame where df[,1] is a vector of two categories (e.g. "A","B"), while df[,2] is a vector of redundant cases (e.g. "Q","Q").
#' @return A 2xk table of counts in the 2 categories.
#' @export
produce_2_by_k_counts_table <- function(df){
df <- changeTypeOfColumns(df,1:2,"character")
two_categories <- unique(df[,1])
if(!(length(two_categories)==2)){
stop("There are more than two categories in df[,1]. Exiting.")
}
k_categories <- unique(df[,2])
out <- matrix(nrow = 2, ncol = length(k_categories))
for(i in 1:length(k_categories)){
category_1 <- nrow(df[df[,1]==two_categories[1] & df[,2]==k_categories[i],]) # count of category 1 & k category i
category_2 <- nrow(df[df[,1]==two_categories[2] & df[,2]==k_categories[i],]) # count of category 2 & k category i
out[1,i] <- category_1
out[2,i] <- category_2
}
row.names(out) <- two_categories
colnames(out) <- k_categories
return(out)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.