Skip to content
Permalink
c5d03d2a60
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
16 lines (15 sloc) 1.26 KB
#' Counts by 2 grouping varaibles
#' @description This function simply groups by two factors and returns the frequency. Can be passed to "use.prop.test" to test proportions.
#' @param df Data frame, in this case meta data of a corresponding gene
#' @param factor_one Character - first grouping factor, a column name of the data frame
#' @param factor_two Character - second grouping factor, a column name of the data frame
#' @importFrom magrittr %>%
#' @examples
#' df <- data.frame(case_id = c(1,2,3,4,5,6), group = c("one","one","two","one","two","three"), primary_diagnosis = c("Squamous cell carcinoma, NOS", "Adenocarcinoma, NOS", "Adenocarcinoma with mixed subtypes", "Squamous cell carcinoma, NOS", "Squamous cell carcinoma, NOS", "Squamous cell carcinoma, NOS"))
#' diagnosis_counts <- count.column(df, factor_one = "group", factor_two = "primary_diagnosis")
#' @return Returns a data frame
count.column <- function(df, factor_one = "group", factor_two = "primary_diagnosis") {
countFactor <- single_data_frame %>% dplyr::group_by_(factor_one, factor_two) %>% #summarize count of patients grouped by group and diagnosis
dplyr::summarize(sum.patients = n()) %>% tidyr::spread(., factor_two, "sum.patients", fill = 0) #spread the df to wide format
return(countFactor)
}