在R中处理嵌套列表

问题描述 投票:0回答:1

我的数据是列表结构列表中的基因,如下所示:

>listoflists <- list(samp1 = c("ENSG00000000003", "ENSG00000000005", "ENSG00000000419", "ENSG00000000457"),
              samp2 = c("ENSG00000002834", "ENSG00000002919", "ENSG00000002933"),
              samp3 = c("ENSG00000000971", "ENSG00000001036", "ENSG00000001084", "ENSG00000001167"))

我正在尝试转换基因标识符。当在数据框结构中处理类似数据时,我已成功使用如下代码:

>library(org.Hs.eg.db)
>gene_df$symbol <- mapIds(org.Hs.eg.db,keys=rownames(gene_df),column="SYMBOL",keytype="ENSEMBL",multiVals="first")

但是现在我正在使用列表列表。我想保持相同的结构,并且我认为提供的答案here应该使我有见识,但是当我尝试使用这样的嵌套Apply命令时:

>convertedLoL <- lapply(listoflists, function(x) lapply(listoflists[x], function(i)mapIds(org.Hs.eg.db,keys=listoflists[i],column="SYMBOL",keytype="ENSEMBL",multiVals="first")))
 Error in listoflists[[i]] : 
  attempt to select less than one element in get1index 

>convertedLoL <- lapply(listoflists, function(x) lapply(listoflists[x], function(i)mapIds(org.Hs.eg.db,keys=listoflists[[x]][[i]],column="SYMBOL",keytype="ENSEMBL",multiVals="first")))
 Error in listoflists[[x]] : no such index at level 1 

我不断出错。我认为我的问题源于我不完全了解应用程序的工作原理和引用列表的事实。有人可以帮我吗?

r nested-lists
1个回答
0
投票

糟糕,我想我已经知道了!

>convertedLoL <- lapply(listoflists, function(x) sapply(x, function(i)mapIds(org.Hs.eg.db,keys=i,column="SYMBOL",keytype="ENSEMBL",multiVals="first")))

很慢,但是它给了我想要的输出!

© www.soinside.com 2019 - 2024. All rights reserved.