Frage

I have the following list (FYI, the list is part of the Bioconductor package "gageData" and can be installed as follow):

source("http://bioconductor.org/biocLite.R")
biocLite("gageData")

and then

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"

The list groups a number of K-IDs into several classes. The K-IDs are not unique, meaning that they can be present in more than one class. I would like to build a dataframe from this list with two columns, the first with UNIQUE K-IDs and the second with the classes where it was originally present. for example the K-id K00170 is present in both groups in the subset above then I would like to have something like the following:

KOID   Class
K00170 ko00010 Glycolysis / Gluconeogenesis; ko00020 Citrate cycle (TCA cycle)
War es hilfreich?

Lösung

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

Andere Tipps

Create a data frame by replicating the list element (Class) names, and by unlist'ing the identifiers (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)

Aggregate the Class column by KOID, using the paste function to collapse classes with the same KOID

aggregate(Class ~ KOID, df, paste, collapse="; ")
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top