我正在尝试使用apply/ mapply/ lapply/ sapply或任何其他方式来矢量化我的嵌套for循环代码以减少运行时间.我的代码如下:
for (i in 1:dim){
for (j in i:dim){
if(mydist.fake[i,j] != d.hat.fake[i,j]){
if((mydist.fake[i,j]/d.hat.fake[i,j] > 1.5)|(d.hat.fake[i,j]/mydist.fake[i,j]>1.5)){
data1 = cbind(rowNames[i],rowNames[j], mydist.fake[i,j], d.hat.fake[i,j], 1)
colnames(data1) = NULL
row.names(data1) = NULL
data = rbind(data, data1)
}else{
data1 = cbind(rowNames[i],rowNames[j], mydist.fake[i,j], d.hat.fake[i,j], 0)
colnames(data1) = NULL
row.names(data1) = NULL
data = rbind(data, data1)
}
}
}
}
write.table(data, file = "fakeTest.txt", sep ="\t", col.names = FALSE, row.names = FALSE)
Run Code Online (Sandbox Code Playgroud)
data 是一个数据帧mydist.fake并且d.hat.fake是距离矩阵(其中对角线为零并且上三角和下三角的值相同)因此,对下三角的横向感兴趣(也留下对角线的值).我有一个100*8的数据矩阵,其中每一行是8个不同时间点的值向量.我很想知道如何在R中绘制下面的矩阵以使图形与下面的图形非常相似:

这是我的数据矩阵的一个例子.
1 2 3 4 5 6 7 8
line1 0.22 0.075 0.35 0.89 0 0.35 0.42 2.34
line2 0 0.47 0.89 2.51 0 0.36 1.14 2.09
line3 1.22 0.075 0.35 0.89 0 0.35 0.42 1.34
line4 2.22 0.75 0.45 0.99 0 0.54 0.24 2.34
line5 3.22 0.275 0.55 0.819 0 0.25 0.34 2.34
Run Code Online (Sandbox Code Playgroud)
任何帮助或建议将受到高度赞赏.谢谢.