【问题标题】:Hierarchical sorting of a table in RR中表格的分层排序
【发布时间】:2020-09-07 15:29:45
【问题描述】:

我有一个看起来像这样但有更多条目的表:

ID     Gene     Tier     Consequence   
1314   ABC      TIER1    missense  
1314   PKD1     TIER1    frameshift 
1314   PKD1     TIER1    stop_gain 
6245   BJD      TIER1    splice_site_variant 
7631   PKD2     TIER1    missense
7631   PKD2     TIER1    non_coding
5336   PKD1     TIER3    missense
1399   PKD1     TIER2    non_coding

我想将结果层次结构子集化为每个 ID 一行,结果层次结构:stop_gain > framshift > splice_site_variant > misense >n on_coding_mutation。实际上,“后果”按等级顺序大致有 10 种。

期望的结果:

ID Gene Tier Consequence
1314 PKD1 TIER1 stop_gain
6245 BJD  TIER1 splice_site_variant
7631 PKD2 TIER1 missense
5336 PKD1 TIER3 missense
1399 PKD1 TIER2 non_coding

我曾想过将结果转化为数字,然后使用它,但想知道是否有办法单独使用文本来做到这一点。我在气闸环境中的 HPC 中工作,因此使用 base R 的解决方案更可取。

非常感谢您的宝贵时间

【问题讨论】:

    标签: r database filter


    【解决方案1】:

    如果您将Consequence 转换为有序因子,您可以在基础 R 中执行此操作并坚持使用文本:

    df$Consequence <- ordered(df$Consequence, 
                              levels = rev(c("stop_gain", "frameshift", 
                                             "splice_site_variant", 
                                             "missense", "non_coding")))
    

    然后您可以通过多种方式获得每个组的最大值。例如,使用split-apply-bind 方法:

    do.call(rbind, lapply(split(df, df$ID), function(x) x[which.max(x$Consequence),]))
    #>        ID Gene  Tier         Consequence
    #> 1314 1314 PKD1 TIER1           stop_gain
    #> 1399 1399 PKD1 TIER2          non_coding
    #> 5336 5336 PKD1 TIER3            missense
    #> 6245 6245  BJD TIER1 splice_site_variant
    #> 7631 7631 PKD2 TIER1            missense
    

    【讨论】:

    • 感谢您花时间回答。这正是我一直在寻找的,我现在已经在有序因子的上下文中学习了水平函数!
    【解决方案2】:

    我建议使用一个因子根据您的标准建立排名,然后使用来自dplyrfilter() 进行子集化。这里是代码。逻辑类似于@AllanCameron 解决方案,但我将因子转换为数字然后过滤:

    library(dplyr)
    #Data
    #Define order
    vord <- c('stop_gain','frameshift','splice_site_variant','missense','non_coding')
    #Format data
    df$Consequence <- factor(df$Consequence,levels = vord,ordered = T)
    #Compute index
    df$Index <- as.numeric(df$Consequence)
    #Filter
    df %>% group_by(ID) %>% filter(Index==min(Index)) %>% select(-Index)
     
    

    输出:

    # A tibble: 5 x 4
    # Groups:   ID [5]
         ID Gene  Tier  Consequence        
      <int> <chr> <chr> <ord>              
    1  1314 PKD1  TIER1 stop_gain          
    2  6245 BJD   TIER1 splice_site_variant
    3  7631 PKD2  TIER1 missense           
    4  5336 PKD1  TIER3 missense           
    5  1399 PKD1  TIER2 non_coding  
    

    使用的一些数据:

    #Data
    df <- structure(list(ID = c(1314L, 1314L, 1314L, 6245L, 7631L, 7631L, 
    5336L, 1399L), Gene = c("ABC", "PKD1", "PKD1", "BJD", "PKD2", 
    "PKD2", "PKD1", "PKD1"), Tier = c("TIER1", "TIER1", "TIER1", 
    "TIER1", "TIER1", "TIER1", "TIER3", "TIER2"), Consequence = structure(c(4L, 
    2L, 1L, 3L, 4L, 5L, 4L, 5L), .Label = c("stop_gain", "frameshift", 
    "splice_site_variant", "missense", "non_coding"), class = c("ordered", 
    "factor")), Index = c(4, 2, 1, 3, 4, 5, 4, 5)), row.names = c(NA, 
    -8L), class = "data.frame")
    

    【讨论】:

    • 感谢您抽出宝贵时间回答。这很好用,但基本 R 解决方案最终更可取,我不能接受两个答案!
    猜你喜欢
    • 1970-01-01
    • 2016-05-24
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    • 2013-10-08
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多