根据逗号分隔的字符向量列的值熔合表(data.frame)

时间:2012-09-27 20:11:05

标签: perl r data.table plyr reshape

我正在进行一项实验,其中我的“区域”包含一些相关的统计信息(实际上有很多其他统计信息和描述性列),以及位于这些区域的以逗号分隔的基因列表。此列表的编号可变,并且可能不包含任何内容(“NA”)。

我如何“融化”表格a:

  region_id  statistic      genelist
          1        2.5       A, B, C
          2        0.5    B, C, D, E
          3        3.2          <NA>
          4        0.1          E, F

为基因列表中的每个基因创建另一个具有单独条目的表格?即。

   region_id statistic gene
           1       2.5    A
           1       2.5    B
           1       2.5    C
           2       0.5    B
           2       0.5    C
           2       0.5    D
           2       0.5    E
           3       3.2 <NA>
           4       0.1    E
           4       0.1    F

我猜是有办法用R / plyr做这个,但我不确定如何。提前谢谢。

编辑:

使用R,您可以使用以下代码重新创建这些玩具向量:

a <- structure(list(region_id = 1:4, statistic = c(2.5, 0.5, 3.2, 
0.1), genelist = structure(c(1L, 2L, NA, 3L), .Label = c("A, B, C", 
"B, C, D, E", "E, F"), class = "factor")), .Names = c("region_id", 
"statistic", "genelist"), class = "data.frame", row.names = c(NA, 
-4L))

b <- structure(list(region_id = c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 
4L, 4L), statistic = c(2.5, 2.5, 2.5, 0.5, 0.5, 0.5, 0.5, 3.2, 
0.1, 0.1), gene = structure(c(1L, 2L, 3L, 2L, 3L, 4L, 5L, NA, 
5L, 6L), .Label = c("A", "B", "C", "D", "E", "F"), class = "factor")), .Names = c("region_id", 
"statistic", "gene"), class = "data.frame", row.names = c(NA, 
-10L))

6 个答案:

答案 0 :(得分:4)

data.table时间,内存和编码效率的解决方案

library(data.table)
DT <- data.table(a)
DT[, list(statistic, 
          gene = unlist(strsplit(as.character(genelist), ', ' ))),
   by = list(region_id)]

或者您可以使用data.table版本&gt; = 1.8.2

中列表的 nice 格式
DTL <- DT[, list(statistic, 
         gene = strsplit(as.character(genelist), ', ' )),
    by = list(region_id)]

DTL
##    region_id statistic    gene
## 1:         1       2.5   A,B,C
## 2:         2       0.5 B,C,D,E
## 3:         3       3.2      NA
## 4:         4       0.1     E,F

在这种情况下,gene是列表

DTL[region_id == 1,unlist(gene)]
## [1] "A" "B" "C"
DTL[region_id == 2,unlist(gene)]
## [1] "B" "C" "D" "E"
# or if the following is of interest
DTL[statistic < 2,unlist(gene)]
## [1] "B" "C" "D" "E" "E" "F"

答案 1 :(得分:2)

简单地分割字段,然后分割基因并打印每个基因一行。您可以在脚本中尝试将<DATA>替换为<>并将输入文件用作perl脚本的参数,例如: perl script.pl input.txt

use strict;
use warnings;

while (<DATA>) {
    chomp;                                   # remove newline
    my ($reg, $stat, $gene) = split /\t/;    # split fields
    my @genes = split /,\s*/, $gene;         # split genes
    for (@genes) {
        local $\ = "\n";                 # adds newline to print
        print join "\t", $reg, $stat, $_;
    }
}

__DATA__
region_id   statistic   genelist
1   2.5 A, B, C
2   0.5 B, C, D, E
3   3.2 <NA>
4   0.1 E, F

<强>输出:

region_id       statistic       genelist
1       2.5     A
1       2.5     B
1       2.5     C
2       0.5     B
2       0.5     C
2       0.5     D
2       0.5     E
3       3.2     <NA>
4       0.1     E
4       0.1     F

答案 2 :(得分:2)

有几种方法可以做到这一点。这种方式有效,尽管可能有更好的方法......

library(stringr) # for str_split
join(subset(a, select=c("region_id", "statistic")), 
     ddply(a, .(region_id), summarise, gene=str_split(genelist, ",\\S*")[[1]]))

需要加载plyr和stringr。

哦,这是一个更好的方法:

ddply(a, .(region_id), 
      function(x) data.frame(gene=str_split(x$genelist, ",\\S*")[[1]], 
                             statistic=x$statistic))

答案 3 :(得分:2)

这是一种没有任何库的方法:

data<-cbind(region_id=1:4, statistic=c(2.5, 0.5, 3.2, 0.1), genelist=c("A, B, C", "B, C, D, E", NA, "E, F"))

do.call(rbind, 
        apply(data, 1, 
              function(r) do.call(expand.grid, 
                                  c(unlist(r[-3]), 
                                    strsplit(r[3], ", ")))))

输出:

      region_id statistic genelist
1          1       2.5        A
2          1       2.5        B
3          1       2.5        C
4          2       0.5        B
5          2       0.5        C
6          2       0.5        D
7          2       0.5        E
8          3       3.2     <NA>
9          4       0.1        E
10         4       0.1        F

答案 4 :(得分:1)

这是使用plyr

的另一个单线程
ddply(a, .(region_id), transform, gene = str_split(genelist, ',')[[1]])

答案 5 :(得分:0)

Perl解决方案:

#!/usr/bin/perl
<>;
print "region_id\tstatistic\tgene\n";
while(<>) {
  chomp;
  my ($reg, $stat, $genes) = split /\s+/, $_, 3;
  foreach my $gene (split /,\s*/, $genes) {
     print "$reg\t$stat\t$gene\n";
  }
}

通过此脚本将原始文件传输到输出文件中。

目前输出值是制表符分隔而不是右冲刷,但如果真的需要,你可以修复它。

相关问题