igraph-help
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[igraph] adding a new edge attribute with 1 or more zero values deletes


From: Tony Larson
Subject: [igraph] adding a new edge attribute with 1 or more zero values deletes all edges
Date: Wed, 31 Oct 2012 16:32:11 +0000
User-agent: Mozilla/5.0 (Windows NT 5.1; rv:16.0) Gecko/20121026 Thunderbird/16.0.2

Thanks Gabor,
That's helpful.

However, another problem I've run across is setting edge attributes. If I add a new numeric edge attribute with a value of 0 for ANY edge (or all, edges but a single 0 is enough), ALL edges are deleted! I've made a workaround by adding a constant positive integer value (e.g. 1) using the g[from = , to = ] method, then replacing with the correct values using E(g)$attribute <- . But obviously something odd is going on. Am I missing something in how to add an edge attribute? See below.


thanks
Tony



#graph from an adjacency matrix with a mix of zeroes and positive double values to indicates edges and their weights g <- graph.adjacency(g.mat, mode = "undirected", weight = TRUE, diag = FALSE)
> g
IGRAPH UNW- 371 15 --
+ attr: name (v/c), weight (e/n)
> el <- get.edgelist(g)
> el
      [,1] [,2]
 [1,] "1"  "54"
 [2,] "2"  "164"
 [3,] "3"  "241"
 [4,] "4"  "272"
 [5,] "5"  "287"
 [6,] "5"  "288"
 [7,] "5"  "291"
 [8,] "6"  "317"
 [9,] "7"  "329"
[10,] "8"  "337"
[11,] "9"  "343"
[12,] "10" "355"
[13,] "11" "364"
[14,] "12" "368"
[15,] "13" "370"

#generating extra attributes to add from some related adjacency matrices
> sim <- apply(el,1,function(x){spec.sim.mat[x[1],x[2]]})
> sim
[1] 1000 1000 1000 1000 968 914 1000 1000 999 999 1000 1000 1000 1000 999
> rtdev <- apply(el,1,function(x){rt.dev.mat[x[1],x[2]]})
> rtdev
 [1] 0.0 0.0 0.0 0.0 5.3 5.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

#adding a new edge attribute called "sim" is OK
> g[from=el[,1], to = el[,2], attr = "sim"] <- sim
> g
IGRAPH UNW- 371 15 --
+ attr: name (v/c), weight (e/n), sim (e/n)
> get.edgelist(g)
      [,1] [,2]
 [1,] "1"  "54"
 [2,] "2"  "164"
 [3,] "3"  "241"
 [4,] "4"  "272"
 [5,] "5"  "287"
 [6,] "5"  "288"
 [7,] "5"  "291"
 [8,] "6"  "317"
 [9,] "7"  "329"
[10,] "8"  "337"
[11,] "9"  "343"
[12,] "10" "355"
[13,] "11" "364"
[14,] "12" "368"
[15,] "13" "370"

#but adding an additional attribute called "rtdev" is not - the attribute is NOT added and all edges are deleted
> g[from=el[,1], to = el[,2], attr = "rtdev"] <- rtdev
> g
IGRAPH UNW- 371 0 --
+ attr: name (v/c), weight (e/n), sim (e/n)
> get.edgelist(g)
     [,1] [,2]

#however, setting the new attribute to a non-zero positive integer first (e.g. 1) then overwriting after works
> g[from=el[,1], to = el[,2], attr = "rtdev"] <- 1
> E(g)$rtdev <- rtdev
> g
IGRAPH UNW- 371 15 --
+ attr: name (v/c), weight (e/n), rtdev (e/n), sim (e/n)
> get.edgelist(g)
      [,1] [,2]
 [1,] "1"  "54"
 [2,] "2"  "164"
 [3,] "3"  "241"
 [4,] "4"  "272"
 [5,] "5"  "287"
 [6,] "5"  "288"
 [7,] "5"  "291"
 [8,] "6"  "317"
 [9,] "7"  "329"
[10,] "8"  "337"
[11,] "9"  "343"
[12,] "10" "355"
[13,] "11" "364"
[14,] "12" "368"
[15,] "13" "370"


> sessionInfo()
R version 2.15.0 (2012-03-30)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=C                 LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

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

other attached packages:
[1] Matrix_1.0-6    lattice_0.20-10 igraph_0.6-2    ape_3.0-5

loaded via a namespace (and not attached):
[1] gee_4.13-18  grid_2.15.0  nlme_3.1-104 tools_2.15.0











On 31/10/2012 15:52, Gábor Csárdi wrote:
Hi,

sorry for the delay. More below.

On Wed, Oct 24, 2012 at 12:26 PM, Tony Larson <address@hidden> wrote:
Hello,
I'm using graph.union.by.name in R to merge graphs with overlapping vertex
symbolic names.  The original graphs also have edge weights, and the way I
made them is such that the edge between any two vertices with matching
symbolic names will always have identical edge weights.  Is there any way,
after running the union, to add back in the weights?  e.g.

g3 <- graph.union.by.name(g1, g2)

I thought of using match in R to identify matching edges thus:

m1 <- match(E(g3), E(g1))
m2 <- match(E(g3), E(g2))

and then using:

E(g3)$weight[m1] <- E(g1)$weight
E(g3)$weight[m2] <- E(g2)$weight

but this fails as match seems to be using the updated edge IDS in the merged
dataset (as expected, I guess).
This will be supported in the coming major release, I am working on it
now. Until then, a workaround is to work with edge lists explicitly:

## make it reproducible
set.seed(4)

## some example data
g1 <- random.graph.game(5, 3/5)
g2 <- random.graph.game(5, 3/5)

V(g1)$name <- letters[1:5]
V(g2)$name <- letters[5:1]

E(g1)$weight <- seq_len(ecount(g1))
E(g2)$weight <- seq_len(ecount(g2))

## the trick is that we might need to swap the columns of the
## edge lists, because they are ordered according to numeric
## vertex ids and not names
reordel <- function(graph) {
   el <- cbind(as.data.frame(get.edgelist(graph), stringsAsFactors=FALSE),
               E(graph)$weight)
   swap <- which(el[,1] > el[,2])
   if (length(swap) > 0) { el[swap,1:2] <- cbind(el[swap,2], el[swap,1]) }
   el
}

el1 <- reordel(g1)
colnames(el1) <- c("from", "to", "weight1")
el2 <- reordel(g2)
colnames(el2) <- c("from", "to", "weight2")

el3 <- merge(el1, el2, all=TRUE)
graph.data.frame(el3, directed=FALSE)

Best,
Gabor

any suggestions would be most welcome

thanks
Tony







_______________________________________________
igraph-help mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/igraph-help









reply via email to

[Prev in Thread] Current Thread [Next in Thread]