gpt4 book ai didi

R:将列表重组为矩阵

转载 作者:行者123 更新时间:2023-12-02 08:49:02 25 4
gpt4 key购买 nike

我有一个结构如下的列表 my.list[[file.id]][value.id]]<-a value(1 or 0) .相同的value.id可以存在于不同的file.id中。

我需要一个矩阵,行名作为所有 value.ids,列名是 file.ids,每个单元格是 my.list[[file.id]][[value.id]] .

有没有一种快速的方法可以做到这一点而不用疯狂地迭代?

示例数据:

列表:

$`Zhou_et_al_2004`
CDC42:P60953 CDK2D:NONAME MAPK12:P53778 E2F3:NONAME GRB2:P62424 GRB2:P62993 RFA:NONAME
"up" "up" "down" "down" "down" "down" "down"
CDK9:P50750 JUP/DP3:NONAME MEK1:NONAME RFC38:NONAME DP2:NONAME RFC37:NONAME GADD45:NONAME
"down" "down" "down" "down" "down" "down" "down"

$`Zhou_et_al_2006`
CTTN:Q14247 GTSE1:Q9NYZ3 CHST11:Q9N CHST11:PF2 TNRC6A:Q8NDV7 MMP9:P14780 NRIP3:Q9N
"up" "up" "up" "up" "up" "up" "up"
NRIP3:Q35 EGFR:P00533 GFPT2:NONAME TPCN2:Q8NHX9 BBP:NONAME SQLE:Q14534 DISP2:NONAME
"up" "up" "up" "up" "up" "up" "up"
PAPPA:Q13219 BMP2:P12643 PCM1:Q15154 SUCLG2:Q96I99 ASAH1:Q13510 UQCRC2:P22695 MTUS1:NONAME
"up" "up" "down" "down" "down" "down" "down"
MUC20:NONAME FRAT2:NONAME PLA2G4A:P47712
"down" "down" "down"

$`Zhou_et_al_2007`
CTTN:Q14247 GTSE1:Q9NYZ3 CHST11:Q9N CHST11:PF2 TNRC6A:Q8NDV7 NRIP3:Q9N
"up" "up" "up" "up" "up" "up"
NRIP3:Q35 USP32:Q8NFA0 PPFIBP1:Q86W92 MALAT1:NONAME TRA2A:NONAME MGC17624:NONAME
"up" "up" "up" "up" "up" "up"
SLC6A2:P23975 USP42:Q9H9J4 RASEF:NONAME SEMA3C:Q99985 NDE1:Q9NXR1 TRA1:NONAME
"up" "up" "up" "up" "up" "up"
PPFIA1:Q13136 PPFIA1:Q16787 ITGA9:Q13797 ITGA9:Q14469 LMO2:P25791 NR2F2:P24468
"up" "up" "down" "down" "down" "down"
KIAA0882:NONAME PCM1:Q15154 CYB5:NONAME IDH1:NONAME MYLIP:Q8WY64 ASAH1:Q13510
"down" "down" "down" "down" "down" "down"
HADHSC:NONAME FAM84B:Q96KN1 ADH5:P11766 NTN4:Q9HB63 AK3:Q9UIJ7 MTUS1:NONAME
"down" "down" "down" "down" "down" "down"
KIAA1815:NONAME
"down"

矩阵:

                Zhou2004 Zhou2006 Zhou2007
CDC42:P60953 "up" NA NA
CDK2D:NONAME "up" NA NA
MAPK12:P53778 "down" NA NA
E2F3:NONAME "down" NA NA
GRB2:P62424 "down" NA NA
GRB2:P62993 "down" NA NA
RFA:NONAME "down" NA NA
CDK9:P50750 "down" NA NA
JUP/DP3:NONAME "down" NA NA
MEK1:NONAME "down" NA NA
RFC38:NONAME "down" NA NA
DP2:NONAME "down" NA NA
RFC37:NONAME "down" NA NA
GADD45:NONAME "down" NA NA
CTTN:Q14247 NA "up" "up"
GTSE1:Q9NYZ3 NA "up" "up"
CHST11:Q9N NA "up" "up"
CHST11:PF2 NA "up" "up"

等(会有更多行)

最佳答案

plyr 包中的

ldply 对于此类任务特别有用。来自文档:

The most unambiguous behaviour is achieved when .fun returns a data frame - in that case pieces will be combined with rbind.fill.*

其中 rbind.fill 是绑定(bind) data.frames 并用 NA 填充缺失数据的便捷函数。

所以这里的技巧是应用一个将列表元素转换为 data.frame 的函数:

my.list <- list()
my.list[["Zhou_et_al_2004"]]["CDC42:P60953"] <- 1
my.list[["Zhou_et_al_2004"]]["CDK2D:NONAME"] <- 2
my.list[["Zhou_et_al_2006"]]["CTTN:Q14247"] <- 3
my.list[["Zhou_et_al_2006"]]["GTSE1:Q9NYZ3"] <- 4
my.list[["Zhou_et_al_2006"]]["CHST11:Q9N"] <- 5

library(plyr)
ldply(my.list, .fun = function(x)as.data.frame(as.list(x)))
# .id CDC42.P60953 CDK2D.NONAME CTTN.Q14247 GTSE1.Q9NYZ3 CHST11.Q9N
# 1 Zhou_et_al_2004 1 2 NA NA NA
# 2 Zhou_et_al_2006 NA NA 3 4 5

我相信您会知道如何将其转换为您的最终格式。

关于R:将列表重组为矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9996262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com