我有以下列表(仅供参考,该列表是Bioconductor软件包“gageData”的一部分,可按如下方式安装):
source("http://bioconductor.org/biocLite.R")
biocLite("gageData")
然后
library(gageData)
data(kegg.sets.ko)
kegg.sets.ko[1:2]
$`ko00010 Glycolysis / Gluconeogenesis`
[1] "K00001" "K00002" "K00016" "K00114" "K00121" "K00128" "K00129" "K00131" "K00134" "K00149" "K00150" "K00161" "K00162" "K00163"
[15] "K00169" "K00170" "K00171" "K00172" "K00382" "K00627" "K00844" "K00845" "K00850" "K00873" "K00886" "K00918" "K00927" "K01084"
[29] "K01085" "K01222" "K01223" "K01568" "K01596" "K01610" "K01622" "K01623" "K01624" "K01689" "K01785" "K01792" "K01803" "K01810"
[43] "K01834" "K01835" "K01837" "K01895" "K01905" "K02446" "K02749" "K02750" "K02752" "K02753" "K02777" "K02778" "K02779" "K02790"
[57] "K02791" "K03738" "K03841" "K04022" "K04041" "K04072" "K05344" "K06859" "K08074" "K10705" "K11389" "K11532" "K11645" "K12406"
[71] "K12407" "K13810" "K13951" "K13952" "K13953" "K13954" "K13980" "K13997" "K14028" "K14029" "K14085" "K15633" "K15634" "K15635"
[85] "K15778" "K15779" "K15916" "K15917" "K16305" "K16306" "K16370"
$`ko00020 Citrate cycle (TCA cycle)`
[1] "K00024" "K00025" "K00026" "K00030" "K00031" "K00161" "K00162" "K00163" "K00164" "K00169" "K00170" "K00171" "K00172" "K00174"
[15] "K00175" "K00176" "K00177" "K00234" "K00235" "K00236" "K00237" "K00239" "K00240" "K00241" "K00242" "K00244" "K00245" "K00246"
[29] "K00247" "K00382" "K00627" "K00658" "K01596" "K01610" "K01643" "K01644" "K01646" "K01647" "K01648" "K01676" "K01677" "K01678"
[43] "K01679" "K01681" "K01682" "K01899" "K01900" "K01902" "K01903" "K01958" "K01959" "K01960" "K13997" "K15230" "K15231"
该列表将多个K-ID分组为几个类。 K-ID不是唯一的,这意味着它们可以存在于多个类中。我想从这个列表中构建一个包含两列的数据帧,第一列是UNIQUE K-ID,第二列是最初出现的类。例如,K-id K00170存在于上面子集中的两个组中,那么我希望有如下内容:
KOID Class
K00170 ko00010 Glycolysis / Gluconeogenesis; ko00020 Citrate cycle (TCA cycle)
答案 0 :(得分:1)
kegg.sets.ko <- list(class_one = 1:5, class_two = 3:10) # Example data to make this copy-pasteable.
df <- data.frame(KOID = unique(c(kegg.sets.ko, recursive = TRUE)))
df$Class <- vapply(df$KOID, function(id)
paste(names(kegg.sets.ko)[vapply(kegg.sets.ko, function(x) id %in% x, logical(1))]
, collapse = '; ')
, character(1))
答案 1 :(得分:1)
通过复制列表元素(类)名称以及取消列出标识符(KOID)来创建数据框
df = data.frame(Class=rep(names(kegg.sets.ko), sapply(kegg.sets.ko, length)),
KOID=unlist(kegg.sets.ko, use.names=FALSE),
stringsAsFactors=FALSE)
按KOID汇总Class列,使用paste函数折叠具有相同KOID的类
aggregate(Class ~ KOID, df, paste, collapse="; ")