【问题标题】:Get unique combination from two columns in R从 R 中的两列获取唯一组合
【发布时间】:2015-12-07 09:59:57
【问题描述】:

我有一个file.csv,格式如下。我需要做的是比较LeftChrRightChr 列并获取uniqe 组合并剥离chr 以获得result 附加t 与文件名的每个唯一组合,如result 所示下面。

>Id LeftChr LeftPosition    LeftStrand  LeftLength  RightChr
4465    chr1    33478980    +   60  chr1
4751    chr1    37908641    +   370 chr2
1690    chr1    37938262    -   112 chr5
4464    chr1    37938376    +   122 chr2
4463    chr2    59097215    +   675 chr2

结果

file.csv:  t(1:1), t(1:2), t(1:5),t(2:2)

【问题讨论】:

    标签: r


    【解决方案1】:

    假设您已将其读入名为 data 的数据框中:

    x = with(data, unique(gsub(pattern = "chr",
                           replacement = "",
                           x = paste("t(", LeftChr, ":", RigthChr, ")"))))
    
    paste("file.csv: ", paste(x, collapse = ", "))
    

    【讨论】:

    • 谢谢。由于我使用 list.files 对多个文件执行此操作,如何在结果中添加文件名?
    • 把它变成一个文件的函数,并在我的paste() 命令中将"file.csv: " 替换为file, ": ",其中file 是您存储文件名的变量。
    【解决方案2】:
    dat <- read.table(text="
    Id LeftChr LeftPosition    LeftStrand  LeftLength  RightChr
    4465    chr1    33478980    +   60  chr1
    4751    chr1    37908641    +   370 chr2
    1690    chr1    37938262    -   112 chr5
    4464    chr1    37938376    +   122 chr2
    4463    chr2    59097215    +   675 chr2
    ", head=T, as.is=T)
    
    dat %>% 
      mutate(lc=gsub("chr", "", LeftChr), rc=gsub("chr", "", RightChr)) %>%
      select(lc, rc) %>%
      group_by(lc, rc) %>%
      unique
    Source: local data frame [5 x 2]
    # Groups: lc, rc [4]
    #
    #      lc    rc
    #   (chr) (chr)
    # 1     1     1
    # 2     1     2
    # 3     1     5
    # 4     2     2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-14
      • 1970-01-01
      • 1970-01-01
      • 2015-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多