【问题标题】:Exclude all rows with JUST specific patterns in R排除 R 中具有特定模式的所有行
【发布时间】:2015-06-29 04:21:04
【问题描述】:

我想排除 df 中只有特定模式(AAABBB)中的所有行。我的真实数据有超过 20k 行和超过 2k 列!遵循一个有代表性的输入示例:

df <- "chr   position sample21s  sample23s sample22s
    chr2    150      AB           BB       AA       
    chr4    250      A            AA       BB
    chr5    350      AB           B        BB   
    chr7    550      AA           AA       AA
    chr8    650      BB           BB       AB"
df <- read.table(text=df, header=T)

预期输出:

chr   position sample21s  sample23s sample22s
chr4    250      A            AA       BB
chr5    350      AB           B        BB   

有什么想法吗?

【问题讨论】:

  • 您能否提供一个可重现的示例:stackoverflow.com/questions/5963269/…
  • 抱歉 -- 您想排除 AA、AB 和BB?在显示的预期输出中,这些行仍然存在。
  • 我想以任何组合或比例排除仅具有 AA、AB 或 BB 的行。如果该行至少有另一种字符串,我需要将该行保留在输出df

标签: r conditional bioconductor


【解决方案1】:

这是另一种选择...

> ind <- apply(df[, grepl("^sample", names(df))], 1,
              function(x) sum(x %in% c("AA", "AB", "BB"))!=3)

> df[ind, ]
   chr position sample21s sample23s sample22s
2 chr4      250         A        AA        BB
3 chr5      350        AB         B        BB

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多