3

我正在做一个实验,其中我有一些相关统计数据(实际上是许多其他统计数据和描述性列)的“区域”,以及位于这些区域中的以逗号分隔的基因列表。该列表的数量可变,并且可能不包含任何内容(“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

为基因列表中的每个基因创建一个单独的条目的另一个表?IE

   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))
4

6 回答 6

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 版本 >= 1.8.2 的列表的漂亮格式

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"

ETC

于 2012-09-27T23:29:34.913 回答
3

只需拆分字段,然后拆分基因并打印每个基因一行。您可以通过替换输入文件并将其用作 perl 脚本的参数在脚本中进行尝试,<DATA>例如.<>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
于 2012-09-27T20:28:11.103 回答
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))
于 2012-09-27T20:29:29.403 回答
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
于 2012-09-28T00:49:03.813 回答
1

Here is another one-liner using plyr

ddply(a, .(region_id), transform, gene = str_split(genelist, ',')[[1]])
于 2012-09-28T14:47:17.247 回答
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";
  }
}

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

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

于 2012-09-27T20:32:41.960 回答