igraph-help
[Top][All Lists]
Advanced

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

Re: [igraph] adding weights after graph.union.by.name


From: Gábor Csárdi
Subject: Re: [igraph] adding weights after graph.union.by.name
Date: Wed, 31 Oct 2012 11:52:14 -0400

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
>



-- 
Gabor Csardi <address@hidden>     MTA KFKI RMKI



reply via email to

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