【问题标题】:Split a data.frame column into others columns将 data.frame 列拆分为其他列
【发布时间】:2017-12-08 10:56:26
【问题描述】:

我有一个很大的data.frame 有一些列,但我的第 9 列是由分号分隔的数据组成的:

    gtf$V9
1                 gene_id CUFF.1; transcript_id CUFF.1.1; FPKM 7.0762407256; frac 1.000000; conf_lo 4.347062; conf_hi 9.805420; cov 25.616962;
2  gene_id CUFF.1; transcript_id CUFF.1.1; exon_number 1; FPKM 7.0762407256; frac 1.000000; conf_lo 4.347062; conf_hi 9.805420; cov 25.616962;
3  gene_id CUFF.1; transcript_id CUFF.1.1; exon_number 2; FPKM 7.0762407256; frac 1.000000; conf_lo 4.347062; conf_hi 9.805420; cov 25.616962;
4  gene_id CUFF.1; transcript_id CUFF.1.1; exon_number 3; FPKM 7.0762407256; frac 1.000000; conf_lo 4.347062; conf_hi 9.805420; cov 25.616962;

所以我想将此列分成其他列,然后将mergedata.frame 的另一部分(第 9 列之前的其他列)一起剪切。

我尝试了一些没有结果的代码:

head(gtf$V9, sep = ";",stringsAsFactors = FALSE) 

new_df <- matrix(gtf$V9, ncol=7, byrow=TRUE) # sep = ";"

as.data.framedata.frameas.matrix 相同

我也尝试过 write.csv 并使用包含 sep=";" 的方式导入它,但 data.frame 太大了,我的电脑运行滞后..

有什么建议吗?

【问题讨论】:

  • 最终read.table(text=gt$V9, sep=';') ...然后cbind()
  • 您正在读取序列变体数据。看看 refGenome 包cran.r-project.org/web/packages/refGenome/vignettes/…
  • 你必须提供更多关于你希望你的理想输出如何的信息。当您将V9 拆分为; 时,您是否获得了列的值? V9 中是否也包含列名?假设后者,我会猜测并发布答案。

标签: r dataframe split multiple-columns


【解决方案1】:

另一个选择是使用splitstackshape-package(它也加载data.table)。使用:

library(splitstackshape)
cSplit(cSplit(df, 'V9', sep = ';', direction = 'long'),
       'V9', sep = ' ')[, dcast(.SD, cumsum(V9_1 == 'gene_id') ~ V9_1)]

给予:

   V9_1  conf_hi  conf_lo       cov exon_number         FPKM     frac gene_id transcript_id
1:    1 9.805420 4.347062 25.616962          NA 7.0762407256 1.000000  CUFF.1      CUFF.1.1
2:    2 9.805420 4.347062 25.616962           1 7.0762407256 1.000000  CUFF.1      CUFF.1.1
3:    3 9.805420 4.347062 25.616962           2 7.0762407256 1.000000  CUFF.1      CUFF.1.1
4:    4 9.805420 4.347062 25.616962           3 7.0762407256 1.000000  CUFF.1      CUFF.1.1

【讨论】:

    【解决方案2】:

    您可以在 sapply() 中执行 strsplit()

    如果您知道 V9 中可以有多少个对象,那么您就可以对其进行 for 循环

    for (i in 1:number_of_max_objects_in_V9) {
     gtf[ncol(gtf)+1] = sapply(1:nrow(gtf), function(x) strsplit(gtf$V9[x],',')[[1]][i])
    }
    

    如果您不知道 V9 可以拥有多少个对象,那么只需在 gtf$V9 中的 , 上运行 str_count,如下所示:

    library(stringr)
    number_of_max_objects_in_V9 <- max(sapply(1:nrow(gtf), function(x) str_count(gtf$V9,',')))
    

    【讨论】:

      【解决方案3】:
      # example dataset (only variable of interest included)
      df = data.frame(V9=c("gene_id CUFF.1; transcript_id CUFF.1.1; FPKM 7.0762407256; frac 1.000000; conf_lo 4.347062; conf_hi 9.805420; cov 25.616962;",
                      "gene_id CUFF.1; transcript_id CUFF.1.1; exon_number 1; FPKM 7.0762407256; frac 1.000000; conf_lo 4.347062; conf_hi 9.805420; cov 25.616962;",
                      "gene_id CUFF.1; transcript_id CUFF.1.1; exon_number 2; FPKM 7.0762407256; frac 1.000000; conf_lo 4.347062; conf_hi 9.805420; cov 25.616962;",
                      "gene_id CUFF.1; transcript_id CUFF.1.1; exon_number 3; FPKM 7.0762407256; frac 1.000000; conf_lo 4.347062; conf_hi 9.805420; cov 25.616962;"),
                      stringsAsFactors = F)
      
      library(dplyr)
      library(tidyr)
      
      df %>%
        mutate(id = row_number()) %>%                  # flag row ids (will need those to reshape data later)                
        separate_rows(V9, sep="; ") %>%                # split strings and create new rows
        separate(V9, c("name","value"), sep=" ") %>%   # separate column name from value
        mutate(value = gsub(";","",value)) %>%         # remove ; when necessary
        spread(name, value)                            # reshape data
      
      #   id  conf_hi  conf_lo       cov exon_number         FPKM     frac gene_id transcript_id
      # 1  1 9.805420 4.347062 25.616962        <NA> 7.0762407256 1.000000  CUFF.1      CUFF.1.1
      # 2  2 9.805420 4.347062 25.616962           1 7.0762407256 1.000000  CUFF.1      CUFF.1.1
      # 3  3 9.805420 4.347062 25.616962           2 7.0762407256 1.000000  CUFF.1      CUFF.1.1
      # 4  4 9.805420 4.347062 25.616962           3 7.0762407256 1.000000  CUFF.1      CUFF.1.1
      

      您可以使用行 ID (id) 将此数据集连接回您的初始数据集。您还需要在原始数据集中创建一个id

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-01-18
        • 1970-01-01
        • 1970-01-01
        • 2012-01-24
        • 2014-12-25
        • 1970-01-01
        • 2018-03-29
        相关资源
        最近更新 更多