【问题标题】:How do I count categorical values in a "+/-" table?如何计算“+/-”表中的分类值?
【发布时间】:2020-09-22 10:18:38
【问题描述】:

我有一个 n 行和 26 列的表,行名称作为基因名称,列作为每个基因的相关功能。如果基因在特定列上有“+”,则表示该基因与该功能相关。对于特定途径,我如何计算所有带有“+”的基因?我试图将图表变成一个数据框,比如 df1,然后使用 summarise 函数,但它没有给我想要的输出。我看到一些帖子说 str_detect() 有效,但这似乎是针对一个变量/列的。 我期望从 Rstudio 获得的一个例子是: 适应性免疫反应 4 血管生成 2 细胞凋亡 4 ....等等等等。 这是我提到的图表的剪辑 (点击链接,级别不够直接上传图片): Gene List

欢迎您提出任何建议的功能或包,对于使用 R 进行数据分析仍然非常新。谢谢

回答关于上传可重现数据的评论:

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")

【问题讨论】:

  • 分享一个可重现的数据示例而不是图像。
  • @sindri_baldur 你这是什么意思?我不明白你的意思,它最初是一个excel电子表格。要我上传excel文件吗?
  • 您的问题被标记为R 。将数据作为data.frame导入R,并使用dput()共享一部分合理的数据。
  • 所以将其输出为 ASCII 文本文件,然后粘贴到堆栈溢出?
  • 是的,只是原始文本……复制数据。将其粘贴并确保格式化为代码。

标签: r dataframe statistics analysis


【解决方案1】:

使用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

【讨论】:

    【解决方案2】:

    如果只有“+”和“-”,您可以执行 colSums(data.frame == "+") 来计算“+”条目的数量。对于那些“-”的人也是如此。下面我排除了我猜不应该包含加号或减号的列,使用 -c(..)

    所以试试这样的:

    counts = cbind(colSums(df[,-c(1,2,26)]=="+"),colSums(df[,-c(1,2,26)]=="-"))
    colnames(counts) = c("plus","minus")
    
                                  plus minus
    Adaptive_Immune_Response         3    32
    Angiogenesis                     1    34
    Apoptosis                        5    30
    Astrocyte_Function               5    30
    Autophagy                        2    33
    Carbohydrate_Metabolism          1    34
    Cell_Cycle                       2    33
    Cellular_Stress                  2    33
    Cytokine_Signaling               6    29
    DNA_Damage                       1    34
    Epigenetic_Regulation            1    34
    Growth_Factor_Signaling          7    28
    Inflammatory_Signaling           6    29
    Innate_Immune_Response           4    31
    Insulin_Signaling                2    33
    Lipid_Metabolism                 0    35
    Matrix_Remodeling                4    31
    Microglia_Function              13    22
    NF.kB                            2    33
    Neurons_and_Neurotransmission    3    32
    Notch                            0    35
    Oligodendrocyte_Function         0    35
    Wnt                              1    34
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-11
      • 1970-01-01
      • 1970-01-01
      • 2014-03-16
      • 2019-09-29
      • 1970-01-01
      • 2020-06-23
      • 2019-05-25
      相关资源
      最近更新 更多