How can you mutate the 10 columns, which contain TRUE if the gene is inside the module and FALSE if it is not?
gene_express = data.frame(gene = c('gene1', 'gene2', 'gene3', 'gene4', 'gene5',
'gene6', 'gene7', 'gene8', 'gene9', 'gene10'), sample1 = sample(0:10,10), sample2 = sample(0:10,10), sample3 = sample(0:10,10), sample4 = sample(0:10,10))
module1 = c('gene1', 'gene2', 'gene10', 'gene8')
module2 = c('gene2', 'gene9', 'gene6', 'gene5', 'gene10')
module3 = c('gene4', 'gene10', 'gene1', 'gene8')
module4 = c('gene5', 'gene8', 'gene2', 'gene7', 'gene6', 'gene5', 'gene10')
module5 = c('gene2', 'gene9', 'gene6', 'gene5', 'gene10')
module6 = c('gene4', 'gene10', 'gene1', 'gene8')
Module_list = list(module1, module2, module3, module4, module5, module6)
names(Module_list) <- c('module1', 'module2', 'module3',
'module4', 'module5', 'module6')
In reality, I have hundreds of these modules, which have been put into a named list of lists, just like my example 'Module_list'. How can I mutate the 'gene_express' data frame such that the module names become new columns containing TRUE if the gene is inside the module and FALSE if not?
The manual way is to specify the module components in the mutate function, as I have here
Current Code
gene_express %>% mutate(
module1 = case_match(gene, c("gene1", "gene2", "gene8", "gene10") ~ TRUE, .default = FALSE),
module2 = case_match(gene, c("gene2", "gene9", "gene6", "gene5", "gene10") ~ TRUE, .default = FALSE),
module3 = case_match(gene, c("gene4", "gene10", "gene1", "gene8") ~ TRUE, .default = FALSE),
module4 = case_match(gene, c("gene2", "gene9", "gene6", "gene5", "gene10") ~ TRUE, .default = FALSE),
module5 = case_match(gene, c("gene4", "gene10", "gene1", "gene8") ~ TRUE, .default = FALSE),
module6 = case_match(gene, c("gene5", "gene2", "gene7", "gene8", "gene6", "gene10") ~ TRUE, .default = FALSE))
What I want is to avoid manually specifying the module in mutate.
Maybe something like this? Here, I put the list of genes by module into a data frame, then we can join to the original data and fill in the non-joined elements with FALSEs.
Result