I have a graph with edge and vertex weights, stored in two data frames: --8<---------------cut here---------------start------------->8--- vertices <- data.frame(vertex=c("a","b","c","d"),weight=c(1,2,1,3)) edges <- data.frame(src=c("a","a","b","c","d"),dst=c("b","c","d","d","a"), weight=c(1,2,1,3,1)) --8<---------------cut here---------------end--------------->8--- I can create a graph from this data: --8<---------------cut here---------------start------------->8--- library(igraph) graph <- graph.data.frame(edges, vertices = vertices, directed=FALSE) --8<---------------cut here---------------end--------------->8--- what I want is to compute some edge weight stats (mean/median/max/min/) for each vertex. I guess I can do something like --8<---------------cut here---------------start------------->8---> sapply(vertices$vertex, function (v) mean(E(g)[inc(v)]$weight))Note: no visible global function definition for 'inc' [1] 1.333333 1.000000 2.500000 1.666667 --8<---------------cut here---------------end--------------->8--- but I was wondering if this is TRT, given the correct answer with a scary note. Thanks! -- Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000 http://www.childpsy.net/ http://palestinefacts.org http://mideasttruth.com http://thereligionofpeace.com http://openvotingconsortium.org If you have no enemies, you are probably dead.
> * Sam Steingold <fqf at tah.bet> [2012-08-15 14:04:18 -0400]: > >> sapply(vertices$vertex, function (v) mean(E(g)[inc(v)]$weight)) > Note: no visible global function definition for 'inc' > [1] 1.333333 1.000000 2.500000 1.666667 > > but I was wondering if this is TRT, given the correct answer with a > scary note.Also, this takes forever and consumes almost all 8GB RAM. It has been running on --8<---------------cut here---------------start------------->8--- IGRAPH DNW- 18590 6734992 -- + attr: name (v/c), count (v/n), weight (e/n) --8<---------------cut here---------------end--------------->8--- for an hour and a half now. -- Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000 http://www.childpsy.net/ http://memri.org http://jihadwatch.org http://www.PetitionOnline.com/tap12009/ http://truepeace.org Please express your antipathy in the suicidal form.
On Wed, Aug 15, 2012 at 2:04 PM, Sam Steingold <sds at gnu.org> wrote:> I have a graph with edge and vertex weights, stored in two data frames: > > --8<---------------cut here---------------start------------->8--- > vertices <- data.frame(vertex=c("a","b","c","d"),weight=c(1,2,1,3)) > edges <- data.frame(src=c("a","a","b","c","d"),dst=c("b","c","d","d","a"), > weight=c(1,2,1,3,1)) > --8<---------------cut here---------------end--------------->8--- > > I can create a graph from this data: > > --8<---------------cut here---------------start------------->8--- > library(igraph) > graph <- graph.data.frame(edges, vertices = vertices, directed=FALSE) > --8<---------------cut here---------------end--------------->8--- > > what I want is to compute some edge weight stats (mean/median/max/min/) > for each vertex. > > I guess I can do something like > > --8<---------------cut here---------------start------------->8--- >> sapply(vertices$vertex, function (v) mean(E(g)[inc(v)]$weight)) > Note: no visible global function definition for 'inc' > [1] 1.333333 1.000000 2.500000 1.666667 > --8<---------------cut here---------------end--------------->8---For me this gives Error in mean(E(g)[inc(v)]$weight) : error in evaluating the argument 'x' in selecting a method for function 'mean': Error in match(x, table, nomatch = 0L) : object 'g' not found which is no wonder, since 'g' is not defined.> but I was wondering if this is TRT, given the correct answer with a > scary note.I am not sure what TRT is, but I would do this as sapply(V(graph), function(v) mean(E(graph)$weight[incident(graph, v)])) Your solution is somewhat messy, because vertices$vertex is actually a factor, etc. Gabor [...] -- Gabor Csardi <csardi at rmki.kfki.hu> MTA KFKI RMKI
On Wed, Aug 15, 2012 at 4:10 PM, Sam Steingold <sds at gnu.org> wrote: [...]> Also, this takes forever and consumes almost all 8GB RAM. > It has been running on > > --8<---------------cut here---------------start------------->8--- > IGRAPH DNW- 18590 6734992 -- > + attr: name (v/c), count (v/n), weight (e/n) > --8<---------------cut here---------------end--------------->8--- > > for an hour and a half now.[...] g <- erdos.renyi.game(18590, 6734992, type="gnm") E(g)$weight <- runif(ecount(g)) and then al <- get.adjedgelist(g) w <- E(g)$weight tmp <- sapply(al, function(e) mean(w[e])) is fairly quick:> system.time(tmp <- sapply(al, function(e) mean(w[e])))user system elapsed 1.353 0.069 1.458 Gabor -- Gabor Csardi <csardi at rmki.kfki.hu> MTA KFKI RMKI