Search code examples
rdataframestatisticsanalysis

How do I count categorical values in a "+/-" table?


I have a table of n rows and 26 cloumns, with the row names as gene names and columns as the associated function of each gene. If the gene has an "+" on a particular column, then it means that the this gene is associated to this function. How do I count all the genes with an "+" for a particular pathway? I tried to turn the chart into a dataframe, say df1 and then use the summarize function, but it didn't give me the output that I wanted. I saw some posts say that str_detect() works, but that seems to be for one varible/column. An example of what I expect to get from Rstudio is: Adaptive Immune Response 4 Angeiogenesis 2 Apoptosis 4 ....so on and so forth. Here's a clipping of the chart that I mentioned (click on the link, level's not enough for direct upload of images): Gene List

Whatever function or package you can suggest would be welcome, still very new to data analysis with R. Thanks

In answer to comment about uploading reproducible data:

structure(list(Gene = c("Cyp27a1", "Tnfrsf13c", "Igf1r", "S100a10", 
"Kit", "Hcar2", "Itgax", "Mbd2", "Asph", "Ccl7", "Dlg1", "Tgm1", 
"Gstm1", "Casp1", "Tbc1d4", "Olfml3", "Ppp3ca", "Igsf10", "Rpl28", 
"Rad1", "F3", "Gpr34", "Lrrc3", "Col6a3", "Cdc7", "Stx18", "Ccl2", 
"Ptx3", "Nfkb1", "Bola2", "Npl", "Itga6", "Slc17a7", "Prkar2a", 
"Serping1"), Cell.Type = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, "Dendritic cells", NA, NA, NA, NA, NA, NA, NA, NA), Adaptive_Immune_Response = c("-", 
"-", "-", "-", "+", "-", "-", "-", "-", "-", "-", "-", "-", "-", 
"-", "-", "+", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", 
"-", "+", "-", "-", "-", "-", "-", "-"), Angiogenesis = c("-", 
"-", "-", "-", "+", "-", "-", "-", "-", "-", "-", "-", "-", "-", 
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", 
"-", "-", "-", "-", "-", "-", "-", "-"), Apoptosis = c("-", "-", 
"+", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "+", "-", 
"-", "+", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", 
"+", "-", "-", "-", "-", "+", "-"), Astrocyte_Function = c("-", 
"-", "-", "+", "-", "-", "-", "-", "-", "-", "-", "+", "-", "-", 
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "+", 
"+", "-", "-", "-", "-", "-", "-", "+"), Autophagy = c("-", "-", 
"+", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", 
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "+", "-", "-", 
"-", "-", "-", "-", "-", "-", "-"), Carbohydrate_Metabolism = c("-", 
"-", "+", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", 
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", 
"-", "-", "-", "-", "-", "-", "-", "-"), Cell_Cycle = c("-", 
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", 
"-", "-", "-", "-", "-", "+", "-", "-", "-", "-", "+", "-", "-", 
"-", "-", "-", "-", "-", "-", "-", "-"), Cellular_Stress = c("-", 
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "+", "-", 
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", 
"-", "+", "-", "-", "-", "-", "-", "-"), Cytokine_Signaling = c("-", 
"+", "-", "-", "+", "-", "-", "-", "-", "+", "-", "-", "-", "+", 
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "+", 
"-", "+", "-", "-", "-", "-", "-", "-"), DNA_Damage = c("-", 
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", 
"-", "-", "-", "-", "-", "+", "-", "-", "-", "-", "-", "-", "-", 
"-", "-", "-", "-", "-", "-", "-", "-"), Epigenetic_Regulation = c("-", 
"-", "-", "-", "-", "-", "-", "+", "-", "-", "-", "-", "-", "-", 
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", 
"-", "-", "-", "-", "-", "-", "-", "-"), Growth_Factor_Signaling = c("-", 
"-", "+", "-", "+", "-", "-", "-", "-", "-", "-", "-", "-", "-", 
"-", "-", "+", "-", "-", "-", "-", "-", "-", "+", "-", "-", "-", 
"-", "+", "-", "-", "+", "-", "+", "-"), Inflammatory_Signaling = c("+", 
"+", "-", "-", "-", "-", "-", "-", "-", "+", "-", "-", "+", "-", 
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "+", 
"-", "+", "-", "-", "-", "-", "-", "-"), Innate_Immune_Response = c("-", 
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "+", 
"-", "-", "+", "-", "-", "-", "-", "-", "-", "-", "-", "-", "+", 
"-", "+", "-", "-", "-", "-", "-", "-"), Insulin_Signaling = c("-", 
"-", "+", "-", "+", "-", "-", "-", "-", "-", "-", "-", "-", "-", 
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", 
"-", "-", "-", "-", "-", "-", "-", "-"), Lipid_Metabolism = c("-", 
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", 
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", 
"-", "-", "-", "-", "-", "-", "-", "-"), Matrix_Remodeling = c("-", 
"-", "-", "-", "-", "-", "+", "-", "-", "-", "-", "-", "-", "-", 
"-", "+", "-", "-", "-", "-", "-", "-", "-", "+", "-", "-", "-", 
"-", "-", "-", "-", "+", "-", "-", "-"), Microglia_Function = c("-", 
"-", "-", "-", "-", "+", "+", "-", "+", "-", "-", "-", "-", "-", 
"+", "-", "-", "+", "+", "-", "+", "+", "+", "+", "-", "-", "-", 
"-", "-", "+", "+", "+", "-", "-", "-"), NF.kB = c("-", "+", 
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", 
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", 
"+", "-", "-", "-", "-", "-", "-"), Neurons_and_Neurotransmission = c("-", 
"-", "-", "-", "-", "-", "-", "-", "-", "-", "+", "-", "-", "-", 
"+", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", 
"-", "-", "-", "-", "-", "+", "-", "-"), Notch = c("-", "-", 
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", 
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", 
"-", "-", "-", "-", "-", "-", "-"), Oligodendrocyte_Function = c("-", 
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", 
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", 
"-", "-", "-", "-", "-", "-", "-", "-"), Wnt = c("-", "-", "-", 
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", 
"+", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", 
"-", "-", "-", "-", "-", "-"), Human_Gene = c("CYP27A1", "TNFRSF13C", 
"IGF1R", "S100A10", "KIT", "HCAR2", "ITGAX", "MBD2", "ASPH", 
"CCL7", "DLG1", "TGM1", "GSTM1", "CASP1", "TBC1D4", "OLFML3", 
"PPP3CA", "IGSF10", "RPL28", "RAD1", "F3", "GPR34", "LRRC3", 
"COL6A3", "CDC7", "STX18", "CCL2", "PTX3", "NFKB1", "BOLA2", 
"NPL", "ITGA6", "SLC17A7", "PRKAR2A", "SERPING1")), row.names = c(181L, 
705L, 314L, 602L, 382L, 285L, 353L, 433L, 24L, 98L, 189L, 680L, 
279L, 85L, 670L, 495L, 537L, 316L, 590L, 568L, 226L, 266L, 405L, 
156L, 131L, 661L, 94L, 562L, 471L, 66L, 484L, 349L, 631L, 546L, 
612L), class = "data.frame")

Solution

  • An option using table():

    other_vars <- c("Gene", "Cell.Type", "Human_Gene")
    pm_vars <- setdiff(names(df), other_vars)
    
    do.call(rbind, lapply(df[pm_vars], table))
    
                                   -  +
    Adaptive_Immune_Response      32  3
    Angiogenesis                  34  1
    Apoptosis                     30  5
    Astrocyte_Function            30  5
    Autophagy                     33  2
    Carbohydrate_Metabolism       34  1
    Cell_Cycle                    33  2
    Cellular_Stress               33  2
    Cytokine_Signaling            29  6
    DNA_Damage                    34  1
    Epigenetic_Regulation         34  1
    Growth_Factor_Signaling       28  7
    Inflammatory_Signaling        29  6
    Innate_Immune_Response        31  4
    Insulin_Signaling             33  2
    Lipid_Metabolism              35 35
    Matrix_Remodeling             31  4
    Microglia_Function            22 13
    NF.kB                         33  2
    Neurons_and_Neurotransmission 32  3
    Notch                         35 35
    Oligodendrocyte_Function      35 35
    Wnt                           34  1