【问题标题】:How can I creat a list from another one, selecting specific conditions using R? [duplicate]如何从另一个列表创建列表,使用 R 选择特定条件? [复制]
【发布时间】:2020-10-14 12:49:00
【问题描述】:

我有一个大数据列表,其中存在 3 个变量:

Conditions Rep RpL Answer
prot1, 1, 1, 53  
prot1, 2, 1, 53  
prot1, 1, 2, 54  
prot1, 2, 2, 36  
prot2, 1, 1, 55  
prot2, 2, 1, 50  
prot2 1 2 62 
...

现在,要选择要绘制的条件,我使用的是简单的:

c<- b[b$Rep %in% c("1", "2", "3"),]

但我想根据同一情节上的条件将重复 (Rep) 分开。

例如,我想选择 prot 1、rep 1、2 和 3(正如我在数组中显示的那样),但对于 prot2,我想选择 rep 3 和 4,在同一个列表中。可能吗? 因为在我做的过程中,我为所有条件选择了代表 1、2 和 3,这不是我想要的。

【问题讨论】:

    标签: r arrays list


    【解决方案1】:

    您可以在subset 中提及条件:

    subset(df, (Conditions == 'prot1' & Rep %in% 1:3) | 
               (Conditions == 'prot2' & Rep %in% 3:4))
    

    或在dplyr 中使用filter

    library(dplyr)
    df %>%
      filter((Conditions == 'prot1' & Rep %in% 1:3) | 
             (Conditions == 'prot2' & Rep %in% 3:4))
    

    【讨论】:

    • 非常感谢!第一个解决了问题!
    猜你喜欢
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 2016-09-11
    • 2018-10-24
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    相关资源
    最近更新 更多