igraph-help
[Top][All Lists]
Advanced

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

Re: [igraph] capturing edge weights in g <- graph.data.frame?


From: Tamás Nepusz
Subject: Re: [igraph] capturing edge weights in g <- graph.data.frame?
Date: Sat, 16 Feb 2013 23:25:57 +0100

> I have a data.frame with two columns, FROM and TO. Each are names of
> people where the person in FROM sent a message to the person in TO.
> There are many cases where person X sends to Y, but graph.data.frame
> doesn't appear to capture edge weight.
Are the weights defined explicitly in your data frame as a third column? If so, 
igraph should add it to the generated graph:

> df <- data.frame(from=c("a", "b", "a"), to=c("b", "a", "c"), weight=c(1,3,1))
> g <- graph.data.frame(df)
> E(g)$weight
[1] 1 3 1

If the weights are not given in the data frame explicitly but you simply want 
to use the number of email exchanges as weights, you can do this:

> df <- data.frame(from=c("a", "b", "a"), to=c("b", "a", "b"))
> g <- graph.data.frame(df)
> E(g)$weight <- 1
> g <- simplify(g, edge.attr.comb="sum")

The call to graph.data.frame will simply create an unweighted graph where the 
number of edges from X to Y will be equal to the number of messages sent by X 
to Y. Then we simply assign a weight of 1 to each message and ask igraph to 
collapse multiple edges into a single one while summing the edge attributes.

-- 
T.




reply via email to

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