Permalink
Cannot retrieve contributors at this time
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?
mmRmeta/R/count.column.R
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
16 lines (15 sloc)
1.26 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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) | |
} |