我试图从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_data_frame $ VAR2 = C(" 10001"" 10001"" 10001"" 10001"" 10001& #34;" 10001"" 10001&#34)
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_data_frame
看起来像这样(我想填写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_matrix
看起来像这样:
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
答案 0 :(得分:1)
您应该阅读Extract
手册页。假设您只使用矩阵中实际存在的行名和列名(与您的示例不同),您可以直接提取值:
x <- matrix(1:25, 5, 5)
colnames(x) <- as.character(1:5)
rownames(x) <- as.character(1:5)
x
# 1 2 3 4 5
# 1 1 6 11 16 21
# 2 2 7 12 17 22
# 3 3 8 13 18 23
# 4 4 9 14 19 24
# 5 5 10 15 20 25
rows <- c("1", "1", "3", "5")
cols <-c("5", "4", "1", "2")
idx <- cbind(rows, cols)
valx <- x[idx]
data.frame(rows, cols, valx)
rows cols valx
# 1 1 5 21
# 2 1 4 16
# 3 3 1 3
# 4 5 2 10