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
謝謝,我已經用拆散() - 適合我的需求,以及將列表的列表向量的列表。 – chronos 2010-08-23 10:12:54