我试图从2000x2000矩阵中提取许多不同的单独点(
bigger_matrix
)。我不需要所有矩阵(因此我不能使用
melt
)但我需要提取特定实例并将其存储在新的预先指定的数据帧中(
output_data_frame
)。目前,我将此作为循环I R:
#example data
bigger_matrix=matrix(0,nrow =5,ncol=5)
rownames(bigger_matrix)=c('10001','10002','10009','100101467','10011')
colnames(bigger_matrix)=c('10001','10002','10009','100101467','10011')
output_data_frame=data.frame(nrow=7)
output_data_frame$Var1=c("10002",'10009','100101467','100129543','100129842','100131017','100131827')
output\u data\u frame$Var2=c(“10001”、“10001”、“10001”、“10001”、“10001”、“10001”、“10001”)
for (i in 1:nrow(output_data_frame)){
p1=as.character(output_data_frame[i,1])
p2=as.character(output_data_frame[i,2])
output_data_framef[i,'value']=bigger_matrix[p1,p2]
#count to monitor progress
print(count)
count=count+1
}
这个过程非常慢。有人知道如何更有效地做到这一点吗?
编辑
output\u data\u帧
看起来像这样(我想填写
NA
s)
row.names Var1 Var2 value
1 1 10002 10001 NA
2 2 10009 10001 NA
3 3 100101467 10001 NA
4 5 100129543 10001 NA
5 6 100129842 10001 NA
6 7 100131017 10001 NA
7 8 100131827 10001 NA
bigger\u矩阵
如下所示:
row.names 10001 10002 10009 100101467 10011
1 10001 0 0 0 0 0
2 10002 0 0 0 0 0
3 10009 0 0 0 0 0
4 100101467 0 0 0 0 0
5 10011 0 0 0 0 0