I need to standardize an oriented graph edges table. The goal is to add to a table (EdgesExport) the potential edges as a pair of nodes (in srctagged & dstagged columns), leaving the new rows void in other columns. To this end I create a matrix of all possible pairs of nodes. And then remove the already existing ones from EdgesExport. Trying one by one works:
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]),])
When I turn this into a loop it works for the few first iterations (3 out of 20 in total), and then delete the complete matrix (which have 56 rows), making an output with 0 rows:
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))
}
I don't get any error or warning message. Here's the sessionInfo() output:
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
The igraph library is loaded at this step of the script. Do you have any idea why doesn't it loop correctly after 3 iterations ? Thanks in advance, Maxime :)
For more information about the data I used here's 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")
And dput(edges2add) before the loop / unique trial (built with a loop rbinding a temporary vector t as you can see) :
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))
I just tried another way, this time collapsing at the 6th iteration. I still can't explain myself why but there is some progress :
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)