繁体   English   中英

将列表转换为 R 中的数据框

[英]Transforming list to data frame in R

对于我所知道的可能是一个非常基本的问题,我深表歉意,但我不知道我正在寻找什么术语来寻找合适的解决方案。 如果您可以将我重定向到另一篇文章,或者帮助我的(复杂的)代码,我将非常感激。

本质上,我有一个巨大的表格,其中包含来自 22 个共表达模块的基因本体结果。 “类”列有 22 个级别,所以我想以 220 行结束。 I could not find an option to limit the output of Gene Ontology terms per module in the anRichment package to the top 10 results, so I am trying to filter this giant table manually, to output only the top 10 hits per module level (if there是 10)

dim(table.display)
[1] 2388   18

table.display[1:5,1:5]
  class rank  dataSetID                     dataSetName inGroups
1 black    1 GO:0007399      nervous system development GO|GO.BP
2 black    2 GO:0045202                         synapse GO|GO.CC
3 black    3 GO:0050808            synapse organization GO|GO.BP
4 black    4 GO:0031175   neuron projection development GO|GO.BP
5 black    5 GO:0048812 neuron projection morphogenesis GO|GO.BP

table.display[2383:2388,1:5]
      class rank  dataSetID                               dataSetName inGroups
2383 yellow   54 GO:0048167         regulation of synaptic plasticity GO|GO.BP
2384 yellow   55 GO:0031226    intrinsic component of plasma membrane GO|GO.CC
2385 yellow   56 GO:0001505     regulation of neurotransmitter levels GO|GO.BP
2386 yellow   57 GO:0051960  regulation of nervous system development GO|GO.BP
2387 yellow   58 GO:0022857        transmembrane transporter activity GO|GO.MF
2388 yellow   59 GO:1903305 regulation of regulated secretory pathway GO|GO.BP

我所做的是:

top10each_final <- list()      # create a new list
for (module in all_modules) {  # for clause to add the top 10 hits of each module to the empty list
  top10each <- table.display[table.display$class==module,]
  top10each_final[[module]] <- top10each[c(1:10),]
} 
top10each_final_2 <- data.frame(matrix(unlist(top10each_final), nrow=length(top10each_final), byrow=T)) # convert the list to a table

当我在 Rstudio 中查看该列表时,该列表看起来正确(列表包含与“类”的每个级别对应的 22 个子列表),但转换为数据框时效果不佳。 我没有得到一个 top10each_final_2 表,其中列表 top10each_final 的元素垂直堆叠,而是得到了几个以奇怪顺序重复的元素。

top10each_final_2[1:5,1:5]
         X1        X2        X3        X4        X5
1     black     black     black     black     black
2      blue      blue      blue      blue      blue
3     brown     brown     brown     brown     brown
4      cyan      cyan      cyan      cyan      cyan
5 darkgreen darkgreen darkgreen darkgreen darkgreen

我想要的最终 output 应该看起来类似于输入,但只包含每个 class 级别的前 10 个命中。 例如:

desired_table.display[1:20,1:5]
      class rank  dataSetID                     dataSetName inGroups
1   black   1   GO:0007399  nervous system development  GO|GO.BP
2   black   2   GO:0045202  synapse GO|GO.CC
3   black   3   GO:0050808  synapse organization    GO|GO.BP
4   black   4   GO:0031175  neuron projection development   GO|GO.BP
5   black   5   GO:0048812  neuron projection morphogenesis GO|GO.BP
6   black   6   GO:0098794  postsynapse GO|GO.CC
7   black   7   GO:0032501  multicellular organismal process    GO|GO.BP
8   black   8   GO:0000902  cell morphogenesis  GO|GO.BP
9   black   9   GO:0005891  voltage-gated calcium channel complex   GO|GO.CC
10  black   10  GO:0048666  neuron development  GO|GO.BP
11  blue    1   GO:0032501  multicellular organismal process    GO|GO.BP
12  blue    2   GO:0009653  anatomical structure morphogenesis  GO|GO.BP
13  blue    3   GO:0035295  tube development    GO|GO.BP
14  blue    4   GO:0007275  multicellular organism development  GO|GO.BP
15  blue    5   GO:0072359  circulatory system development  GO|GO.BP
16  blue    6   GO:0035239  tube morphogenesis  GO|GO.BP
17  blue    7   GO:0048856  anatomical structure development    GO|GO.BP
18  blue    8   GO:0048646  anatomical structure formation involved in morphogenesis    GO|GO.BP
19  blue    9   GO:0072358  cardiovascular system development   GO|GO.BP
20  blue    10  GO:1990837  sequence-specific double-stranded DNA binding   GO|GO.MF

任何想法将不胜感激!

谢谢!

我只是复制并粘贴回答我问题的评论,谢谢@jav!

“也许您需要做的(将列表转换为数据框)是 do.call(rbind, top10each_final)。也就是说,如果您要执行 top10each_final = table.display[table.display $rank %in% 1:10,],这会给你你需要的东西吗(我正在过滤排名为 1、2、...、10 的任何情况)"

您需要做的(将列表转换为数据框)是

do.call(rbind, top10each_final)

也就是说,如果您要执行以下操作,而不是上面的循环:

top10each_final = table.display[table.display$rank %in% 1:10,]

这会给你你所需要的(我正在过滤排名为1, 2, ..., 10的任何情况)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM