【问题标题】:Split a column of values delimited by colons into separate columns for each value [duplicate]将由冒号分隔的一列值拆分为每个值的单独列[重复]
【发布时间】:2014-02-05 01:07:26
【问题描述】:

我有一个如下的刺痛和数字表:

           V1                  V2
1  GT:AD:DP:GQ:PL  0/1:10,45:55:70:106,0,70
2  GT:AD:DP:GQ:PL  1/1:2,42:44:16:288,16,0
3  GT:AD:DP:GQ:PL  1/1:3,37:40:14:147,14,0
4  GT:AD:DP:GQ:PL  0/1:7,50:57:55:250,0,55

对于向量V2,我想将':'-分隔(冒号分隔)值拆分为每个值的单独列,例如

   V1              V2   V3     V4  V5  V6
1  GT:AD:DP:GQ:PL  0/1  10,45  55  70  106,0,70

【问题讨论】:

    标签: r split delimiter


    【解决方案1】:

    另一种方法

    data.frame(DF$V1, do.call(rbind, strsplit(DF$V2, split = ":", fixed = TRUE)))
    ##            DF.V1  X1    X2 X3 X4       X5
    ## 1 GT:AD:DP:GQ:PL 0/1 10,45 55 70 106,0,70
    ## 2 GT:AD:DP:GQ:PL 1/1  2,42 44 16 288,16,0
    ## 3 GT:AD:DP:GQ:PL 1/1  3,37 40 14 147,14,0
    ## 4 GT:AD:DP:GQ:PL 0/1  7,50 57 55 250,0,55
    

    【讨论】:

      【解决方案2】:

      我在我的“splitstackshape”包中包含了一系列名为concat.split 的函数,其中一个是concat.split.multiple。在幕后,它就像@agstudy 的答案,但允许您一次拆分多个列。

      用法很简单:

      library(splitstackshape)
      ### Three required arguments: The input dataset,
      ###   a vector of the columns that need to be split up
      ###   (can also be the numeric column position), and the 
      ###   separator that should be used (can be different 
      ###   for each column).
      concat.split.multiple(data = dat, split.cols = c("V2"), seps = ":")
      #               V1 V2_1  V2_2 V2_3 V2_4     V2_5
      # 1 GT:AD:DP:GQ:PL  0/1 10,45   55   70 106,0,70
      # 2 GT:AD:DP:GQ:PL  1/1  2,42   44   16 288,16,0
      # 3 GT:AD:DP:GQ:PL  1/1  3,37   40   14 147,14,0
      # 4 GT:AD:DP:GQ:PL  0/1  7,50   57   55 250,0,55
      

      另请参阅this answerthis Gist,了解该功能的发展方向。 “data.table”变体在较大的数据集上会快得多,但数据必须是“矩形”(即拆分后的结果列数必须平衡)。

      【讨论】:

        【解决方案3】:

        使用 read.table 两次,使用 2 个不同的分隔符:

        txt = '           V1                  V2
        1  GT:AD:DP:GQ:PL  0/1:10,45:55:70:106,0,70
        2  GT:AD:DP:GQ:PL  1/1:2,42:44:16:288,16,0
        3  GT:AD:DP:GQ:PL  1/1:3,37:40:14:147,14,0
        4  GT:AD:DP:GQ:PL  0/1:7,50:57:55:250,0,55'
        
        ## here replace text=txt with your file name
        dat <- read.table(text=txt,header=TRUE,stringsAsFactors=FALSE)
        data.frame(x1=dat$V1,read.table(text=dat$V2,sep=':'))
        
                      x1  V1    V2 V3 V4       V5
        1 GT:AD:DP:GQ:PL 0/1 10,45 55 70 106,0,70
        2 GT:AD:DP:GQ:PL 1/1  2,42 44 16 288,16,0
        3 GT:AD:DP:GQ:PL 1/1  3,37 40 14 147,14,0
        4 GT:AD:DP:GQ:PL 0/1  7,50 57 55 250,0,55
        

        【讨论】:

        • @Jeremy 我不明白你的意思。看起来你试图谈论一个一般情况,但我不清楚(至少你描述它的方式是 vcf 格式)。您假设某些内容不在 OP 中(我无法猜测 OP 有其他列)..也许您可以在答案中添加一个示例 ...
        • 是的,我对数据的了解比 OP 提供的要多。它是变异检出文件 (vcf) 中的两列,它是通过将基因序列数据与参考和检出变体进行比对而获得的。您的解决方案适用于给定的示例数据。
        • @Jeremy 我的意思是,如果您提供通用的 fil 格式来调整我的解决方案(即使我看不出它是如何失败的),这将是有帮助的,更有趣的是
        • 哦对了,这个网站的前几行有一个:1000genomes.org/wiki/Analysis/Variant%20Call%20Format/…
        【解决方案4】:

        调用该表vcf

        vcf.info <- data.frame(t(sapply(vcf[,2], function(y) strsplit(y,split=":")[[1]])))
        

        然后cbind 使用您想要的原始 vcf 列

        vcf.info2 <- cbind(vcf[,1],vcf.info)
        

        但在真正的 vcf 中我会

        vcf.info2 <- cbind(vcf[,c(1,2,4,5,6,8,9)],vcf.info)
        

        您可能会发现其他有用的东西,在这种情况下,我只是获取读取深度,将 n 替换为您拥有的任意数量的样本,并将 3 替换为 GT、AD、DP、GQ、PL 的 1 到 5

        selectReadDepth <- apply(vcf[,10:n],2,function(x) sapply(x, function(y) strsplit(y,split=":")[[1]][3]))
        

        【讨论】:

          猜你喜欢
          • 2018-11-01
          • 2023-03-22
          • 2018-07-27
          • 1970-01-01
          • 1970-01-01
          • 2021-08-27
          • 2012-01-28
          • 1970-01-01
          相关资源
          最近更新 更多