【问题标题】:Extract specific rows using R使用 R 提取特定行
【发布时间】:2013-11-18 10:20:01
【问题描述】:

我有一个 ID 向量,用于描述组的成员身份。每个 ID 在列表中只出现一次。

例子:

GO:0006169
GO:0032238
GO:0046086
GO:0006154
GO:0046085
GO:0004001

我还有一个表(3 列,74985 行,无标题),其中包含 V1 中的个人 ID(记录为数字),V2 中的组 ID。以及 V3 中该组的简短描述。

例子:

1 GO:0003674                                           molecular_function
1 GO:0005576                                         extracellular region
1 GO:0008150                                           biological_process
2 GO:0001869 negative regulation of complement activation, lectin pathway
2 GO:0004867                 serine-type endopeptidase inhibitor activity
2 GO:0005515                                              protein binding

每个人可以属于多个组,每个组中可以有多个个人。在示例中,个人 1 在组 GO:0003674, GO:0005576 and GO:0008150 中。

我想从表中提取并保留组 ID 与组 ID 向量匹配的每一行(即每个组)。第一个向量中的一些 ID 在表中没有匹配项。我尝试过使用合并功能,但没有成功,它似乎在一个组中多次包含同一个人。

【问题讨论】:

    标签: r genetics


    【解决方案1】:

    我猜你的表是指数据框 - 如果不是,只需转换并可能使用 names() 调整列名或使用索引。

    使用which() 在 df 中查找索引,然后使用这些索引提取适当的行:

    > df <- data.frame(g=1:10,v=1:10)
    > v <- c(3,4,7,33)
    > df[df$g %in% v,]
      g v
    3 3 3
    4 4 4
    7 7 7
    

    另一种选择是使用sqldf,然后使用 SQL 处理数据帧,如表。

    【讨论】:

    • which() 是不必要的 - %in% 返回一个逻辑向量,df[df$g %in% v,] 会这样做
    • 这是一个数据框是的。它给了我一些东西,但我在每个组中的结果似乎比初始数据框中的人数少。 New_data=old_data[old_data$V2 %in% Vector_of_ID,]
    【解决方案2】:

    使用merge

    #dummy - GO dataframe
    df1 <- read.table(text="GO:0006169
    GO:0032238
    GO:0046086
    GO:0006154
    GO:0046085
    GO:0004001",col.names=c("GO_ID"))
    
    #dummy - sample
    df2 <- read.table(text="
    1 GO:0003674 molecular_function
    1 GO:0046086 extracellular_region
    1 GO:0008150 biological_process
    1 GO:0046085 xxx
    2 GO:0046085 negative_xx_lectinpathway
    2 GO:0004867 serine-type_endopeptidase_inhibitor
    2 GO:0005515 protein_binding",col.names=c("Sample_ID","GO_ID","Description"))
    
    #output
    merge(df1,df2)
    #GO_ID Sample_ID               Description
    #1 GO:0046085         1                       xxx
    #2 GO:0046085         2 negative_xx_lectinpathway
    #3 GO:0046086         1      extracellular_region
    

    【讨论】:

    • +1 但这不会覆盖多个 ID,或者它会返回一个 Sample_ID 和 Sample_ID_2 然后崩溃,如果我没记错的话。
    • 如果 Sample_ID 1 的 GO:0046085 和 Sample_ID 相同,会发生什么?在那种情况下,我认为它会像我在之前的评论中所说的那样
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多