0

我需要标准化一个有向图边表。目标是将潜在边作为一对节点(在 srctagged 和 dstagged 列中)添加到表 (EdgesExport) 中,使其他列中的新行无效。为此,我创建了一个包含所有可能的节点对的矩阵。然后从 EdgesExport 中删除已经存在的。尝试一项一项工作:

head(edges2add)   
t=c(EdgesExport$srctagged[1],EdgesExport$dstagged[1])
t #check this is ok
head(edges2add[-(edges2add[,1]==t[1] & edges2add[,2]==t[2]),])

当我把它变成一个循环时,它适用于少数第一次迭代(总共 20 次中的 3 次),然后删除完整的矩阵(有 56 行),输出 0 行:

for (i in 1:nrow(EdgesExport))
    {t=c(EdgesExport$srctagged[i],EdgesExport$dstagged[i])
     print(t)
     edges2add = edges2add[-(edges2add[,1]==t[1] & edges2add[,2]==t[2]),]
     print(nrow(edges2add))
     print(head(edges2add))
     }

我没有收到任何错误或警告消息。这是 sessionInfo() 输出:

R version 3.6.3 (2020-02-29)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=French_France.1252  LC_CTYPE=French_France.1252    LC_MONETARY=French_France.1252
[4] LC_NUMERIC=C                   LC_TIME=French_France.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_3.6.3 tools_3.6.3

igraph库在脚本的这一步加载。您知道为什么在 3 次迭代后它不能正确循环吗?在此先感谢,马克西姆 :)

有关我在这里使用的数据的更多信息是 dput(EdgesExport):

structure(list(X = 0:19, src = c(0L, 0L, 0L, 0L, 3L, 9L, 9L, 
9L, 16L, 16L, 17L, 17L, 23L, 23L, 23L, 27L, 27L, 41L, 41L, 41L
), dst = c(3L, 9L, 23L, 16L, 0L, 16L, 17L, 0L, 23L, 27L, 3L, 
27L, 16L, 41L, 0L, 41L, 16L, 3L, 23L, 16L), rules = structure(c(8L, 
14L, 5L, 15L, 4L, 3L, 12L, 9L, 9L, 13L, 11L, 2L, 6L, 8L, 10L, 
11L, 7L, 10L, 1L, 7L), .Label = c("R14,R33,R46,R34", "R19", "R19,R40", 
"R33,R14,R34", "R43", "R45,R44,R9,R31,R11,R27,R12,R46,R26,R43", 
"R46", "R47,R66,R56,R51", "R52,R64,R53", "R58,R50,R59,R57", "R64,R53", 
"R65,R66,R56", "R65,R66,R56,R42,R51", "R9,R26", "R9,R44,R27,R12,R26,R43"
), class = "factor"), labels = c("91.rSd,81.rNg,75.rFi,69.h", 
"12.dIc,38.dRc", "61.dVe", "12.dIc,15.dIc,61.dVe,62.dVe,38.dRc,39.dRc,61.dVe,62.dVe", 
"45.dSd,46.dSd,17.dLi,45.dSd,46.dSd", "24.dPi,58.dTr", "90.rSd,91.rSd,81.rNg", 
"76.rIc,89.rRc,77.rIc", "76.rIc,77.rIc,89.rRc", "90.rSd,91.rSd,81.rNg,75.rFi,60.dTr", 
"77.rIc,89.rRc", "24.dPi", "12.dIc,64.dVe,38.dRc,39.dRc,43.dRc,38.dRc,43.dRc,64.dVe,14.dIc,43.dRc,38.dRc,43.dRc,64.dVe,12.dIc,14.dIc,38.dRc,43.dRc,12.dIc,15.dIc,61.dVe,62.dVe,65.dVe,38.dRc,39.dRc,65.dVe,12.dIc,14.dIc,15.dIc,61.dVe,62.dVe,12.dIc,14.dIc,15.dIc,61.dVe,62.dVe,65.dVe,12.dIc,15.dIc,61.dVe,62.dVe,14.dIc,61.dVe,62.dVe,65.dVe,61.dVe,62.dVe,65.dVe,14.dIc,61.dVe,62.dVe,61.dVe,62.dVe", 
"91.rSd,81.rNg,75.rFi,69.h", "83.rPi,73.rDn,82.rPi,82.rPi,83.rPi,84.rPi", 
"77.rIc,89.rRc", "65.dVe", "83.rPi,82.rPi,82.rPi,83.rPi,84.rPi,73.rDn", 
"45.dSd,17.dLi,46.dSd,45.dSd,46.dSd,65.dVe", "65.dVe"), ID = c("69, 75, 81, 91", 
"12, 38", "61", "12, 15, 38, 39, 61, 62", "17, 45, 46", "24, 58", 
"81, 90, 91", "76, 77, 89", "76, 77, 89", "60, 75, 81, 90, 91", 
"77, 89", "24", "12, 14, 15, 38, 39, 43, 61, 62, 64, 65", "69, 75, 81, 91", 
"73, 82, 83, 84", "77, 89", "65", "73, 82, 83, 84", "17, 45, 46, 65", 
"65"), Acronym = c("h, rFi, rNg, rSd", "dIc, dRc", "dVe", "dIc, dRc, dVe", 
"dLi, dSd", "dPi, dTr", "rNg, rSd", "rIc, rRc", "rIc, rRc", "dTr, rFi, rNg, rSd", 
"rIc, rRc", "dPi", "dIc, dRc, dVe", "h, rFi, rNg, rSd", "rDn, rPi", 
"rIc, rRc", "dVe", "rDn, rPi", "dLi, dSd, dVe", "dVe"), srctagged = c("None", 
"None", "None", "None", "Degr1", "Distr", "Distr", "Distr", "DistrDegr1", 
"DistrDegr1", "Prod", "Prod", "ProdDegr1", "ProdDegr1", "ProdDegr1", 
"ProdDistr", "ProdDistr", "ProdDistrDegr1", "ProdDistrDegr1", 
"ProdDistrDegr1"), dstagged = c("Degr1", "Distr", "ProdDegr1", 
"DistrDegr1", "None", "DistrDegr1", "Prod", "None", "ProdDegr1", 
"ProdDistr", "Degr1", "ProdDistr", "DistrDegr1", "ProdDistrDegr1", 
"None", "ProdDistrDegr1", "DistrDegr1", "Degr1", "ProdDegr1", 
"DistrDegr1")), row.names = c(NA, -20L), class = "data.frame")

在循环/唯一试验之前 dput(edges2add) (如您所见,使用循环rbind临时向量 t 构建):

structure(c("None", "None", "None", "None", "None", "None", "None", 
"Degr1", "Degr1", "Degr1", "Degr1", "Degr1", "Degr1", "Degr1", 
"Distr", "Distr", "Distr", "Distr", "Distr", "Distr", "Distr", 
"DistrDegr1", "DistrDegr1", "DistrDegr1", "DistrDegr1", "DistrDegr1", 
"DistrDegr1", "DistrDegr1", "Prod", "Prod", "Prod", "Prod", "Prod", 
"Prod", "Prod", "ProdDegr1", "ProdDegr1", "ProdDegr1", "ProdDegr1", 
"ProdDegr1", "ProdDegr1", "ProdDegr1", "ProdDistr", "ProdDistr", 
"ProdDistr", "ProdDistr", "ProdDistr", "ProdDistr", "ProdDistr", 
"ProdDistrDegr1", "ProdDistrDegr1", "ProdDistrDegr1", "ProdDistrDegr1", 
"ProdDistrDegr1", "ProdDistrDegr1", "ProdDistrDegr1", "Degr1", 
"Distr", "DistrDegr1", "Prod", "ProdDegr1", "ProdDistr", "ProdDistrDegr1", 
"None", "Distr", "DistrDegr1", "Prod", "ProdDegr1", "ProdDistr", 
"ProdDistrDegr1", "None", "Degr1", "DistrDegr1", "Prod", "ProdDegr1", 
"ProdDistr", "ProdDistrDegr1", "None", "Degr1", "Distr", "Prod", 
"ProdDegr1", "ProdDistr", "ProdDistrDegr1", "None", "Degr1", 
"Distr", "DistrDegr1", "ProdDegr1", "ProdDistr", "ProdDistrDegr1", 
"None", "Degr1", "Distr", "DistrDegr1", "Prod", "ProdDistr", 
"ProdDistrDegr1", "None", "Degr1", "Distr", "DistrDegr1", "Prod", 
"ProdDegr1", "ProdDistrDegr1", "None", "Degr1", "Distr", "DistrDegr1", 
"Prod", "ProdDegr1", "ProdDistr"), .Dim = c(56L, 2L), .Dimnames = list(
    c("t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", 
    "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", 
    "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", 
    "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", "t", 
    "t", "t", "t", "t", "t", "t", "t", "t", "t"), NULL))

我只是尝试了另一种方式,这次在第 6 次迭代时崩溃。我仍然无法解释自己为什么,但有一些进展:

edges2add=as.matrix(edges2add)
t=matrix(nrow=20,ncol=2)
t[,1]=EdgesExport$srctagged
t[,2]=EdgesExport$dstagged

str(t)
t[1,2]
EdgesExport$dstagged[1]
str(edges2add[-(edges2add[6,]==t[6,]),])

for (i in 1:nrow(t)) {edges2add = edges2add[-(edges2add[i,]==t[i,]),]
                      print(t[i,])
                      print(nrow(edges2add))
                      print(head(edges2add))
}
str(edges2add)
4

0 回答 0