【问题标题】:R: filter dataset based on unique columns [duplicate]R:基于唯一列过滤数据集[重复]
【发布时间】:2011-08-28 21:45:19
【问题描述】:

可能重复:
R: Finding patterns across multiple columns- possibly duplicated()?

亲爱的,

这是我的数据集的一部分:

         name   chr     start      stop strand   alias 
60 uc003vqx.2  chr7 130835560 130891916      -   PODXL
61 uc003xlp.1  chr8  38387812  38445509      -     FLG
62 uc003xlu.1  chr8  38400008  38445509      -     FLG
63 uc003xlv.1  chr8  38400008  38445509      -     FLG
64 uc003xtz.1  chr8  61263976  61356508      -     CA8
65 uc003xua.1  chr8  61283183  61356508      -     CA8
66 uc010lwg.1  chr8  38387812  38445509      -     FLG
67 uc010lwh.1  chr8  38387812  38445509      -     FLG
68 uc010lwj.1  chr8  38387812  38445509      -     FLG

我想根据唯一的开始、停止和别名列过滤数据集。最终的结果一定是这样的:

         name   chr     start      stop strand   alias 
60 uc003vqx.2  chr7 130835560 130891916      -   PODXL
61 uc003xlp.1  chr8  38387812  38445509      -     FLG
62 uc003xlu.1  chr8  38400008  38445509      -     FLG
64 uc003xtz.1  chr8  61263976  61356508      -     CA8
65 uc003xua.1  chr8  61283183  61356508      -     CA8
66 uc010lwg.1  chr8  38387812  38445509      -     FLG

有谁知道这个问题有没有解决办法? 谢谢!

【问题讨论】:

标签: r select unique subset


【解决方案1】:

我认为您的示例输出有误,请尝试

dfrm$comb <-  with(dfrm, paste(start,stop, alias, sep="+"))
dfrm[!duplicated(dfrm$comb), 1:6]
#---
         name  chr     start      stop strand alias
60 uc003vqx.2 chr7 130835560 130891916      - PODXL
61 uc003xlp.1 chr8  38387812  38445509      -   FLG
62 uc003xlu.1 chr8  38400008  38445509      -   FLG
64 uc003xtz.1 chr8  61263976  61356508      -   CA8
65 uc003xua.1 chr8  61283183  61356508      -   CA8

【讨论】:

  • 虽然这是一个实用的解决方案(我已经在可怕的 Excel 中多次使用过这个解决方案),但应该可以在这不起作用的地方构建假设数据。例如,想象一个数据集,其中每列由不同数量的 + 符号组成。
  • 当然。你的方法要好得多。
【解决方案2】:

使用duplicated函数:

复制数据:

x <- "         name   chr     start      stop strand   alias 
60 uc003vqx.2  chr7 130835560 130891916      -   PODXL
61 uc003xlp.1  chr8  38387812  38445509      -     FLG
62 uc003xlu.1  chr8  38400008  38445509      -     FLG
63 uc003xlv.1  chr8  38400008  38445509      -     FLG
64 uc003xtz.1  chr8  61263976  61356508      -     CA8
65 uc003xua.1  chr8  61283183  61356508      -     CA8
66 uc010lwg.1  chr8  38387812  38445509      -     FLG
67 uc010lwh.1  chr8  38387812  38445509      -     FLG
68 uc010lwj.1  chr8  38387812  38445509      -     FLG"

dat <- read.table(textConnection(x), header=TRUE)

删除重复项:

dat[!duplicated(dat[, c("start", "stop", "alias")]), ]

         name  chr     start      stop strand alias
60 uc003vqx.2 chr7 130835560 130891916      - PODXL
61 uc003xlp.1 chr8  38387812  38445509      -   FLG
62 uc003xlu.1 chr8  38400008  38445509      -   FLG
64 uc003xtz.1 chr8  61263976  61356508      -   CA8
65 uc003xua.1 chr8  61283183  61356508      -   CA8

【讨论】:

  • 我之前用过复制功能,不知道这个也可以。谢谢!
猜你喜欢
  • 2017-03-24
  • 1970-01-01
  • 1970-01-01
  • 2019-06-24
  • 1970-01-01
  • 1970-01-01
  • 2020-12-22
  • 2016-03-12
  • 1970-01-01
相关资源
最近更新 更多