2010-08-22 36 views
2

我有一个长长的清单E2I,其中“地图” rownames到价值观,并具有重复rownames:如何将具有非唯一rownames的列表转换为具有唯一rownames的(嵌套)列表?

> head(e2i) 
$`679594` 
[1] "IPR019956" 

$`679594` 
[1] "IPR019954" 

$`679594` 
[1] "IPR019955" 

$`679594` 
[1] "IPR000626" 

$`682397` 
[1] "IPR019956" 

$`682397` 
[1] "IPR019954" 

我需要将其转换成具有独特rownames,其中每个命名的元素将是列表的列表(无论是有名还是无名)值:

> str(mylist) 
List of 2 
$ 679594:List of 3 
    ..$ : chr "IPR019956" 
    ..$ : chr "IPR019954" 
    ..$ : chr "IPR019955" 
$ 682397:List of 2 
    ..$ : chr "IPR019956" 
    ..$ : chr "IPR019954" 

我相信有一个简短而优雅的解决方案。

至于长和丑陋的解决方案 - 我想我能做到这一点,像这样一个循环:

mytest = function(e2i) { 
    result = list() 
    for (e in names(e2i)) { 
      # iterate all rownames, including duplicates 
      if (e %in% names(result)) { 
        # convert existing element to a list (if not already a list), 
        # then append new value e2i[[e]] to that nested list 
      } 
      else { 
        # just add the value to the result 
        result = c(result, e2i[[e]]) 
      } 
    } 
    return(result) 
} 

最初的数据是一个矩阵,为我解决循环上述草案我会用它作为输入:

> head(entrez2interpro_matrix) 
    EntrezGene.ID Interpro.ID 
1  679594 IPR019956 
2  679594 IPR019954 
3  679594 IPR019955 
4  679594 IPR000626 
5  682397 IPR019956 
6  682397 IPR019954 

回答

2

你看过reshape包吗?

或者只是使用unstack()

> d 
    EntrezGene.ID Interpro.ID 
1  679594 IPR019956 
2  679594 IPR019954 
3  679594 IPR019955 
4  679594 IPR000626 
5  682397 IPR019956 
6  682397 IPR019954 
> unstack(d, Interpro.ID ~ EntrezGene.ID) 
$`679594` 
[1] "IPR019956" "IPR019954" "IPR019955" "IPR000626" 

$`682397` 
[1] "IPR019956" "IPR019954" 
+0

谢谢,我已经用拆散() - 适合我的需求,以及将列表的列表向量的列表。 – chronos 2010-08-23 10:12:54

相关问题