igraph-help
[Top][All Lists]
Advanced

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

Re: [igraph] compute and assign attributes to graphs in a list


From: Gábor Csárdi
Subject: Re: [igraph] compute and assign attributes to graphs in a list
Date: Mon, 11 Mar 2013 10:32:50 -0400

Hi Yannick,

On Mon, Mar 11, 2013 at 7:28 AM, Yannick Rochat <address@hidden> wrote:
Dear all,


I have lists in which elements are graphs, like :

graphs <- lapply(c(100,1000), graph.atlas)


In order to plot them, I would like to compute sizes of vertices depending on degree, color, labels, etc. Idem with edges and layouts. I have found a way to do that with the apply family, but it is not elegant and I'm not confident about it :

graphs <- lapply(graphs, function(x) {V(x)$size <- degree(x) ; x})

This is about the best you can do here. You cannot simply do 

graphs <- lapply(graphs, function(x) { V(x)$size <- degree(x) })

because then your anonymous function returns V(x)$size and that is put into 'graphs'. This is not specific to igraph, all replacement functions are like that IMHO:

v <- 1:5
x <- length(v) <- 10
v
#  [1]  1  2  3  4  5 NA NA NA NA NA
x
# [1] 10

Finally, I've decided to use 'for', but found myself with a problem I don't understand. Why this

for (i in 1:length(graphs)) V(graphs[[i]])$size <- degree(graphs[[i]])

works is clear to me, but why that

for (g in graphs) V(g)$size <- degree(g)

doesn't isn't (in print(graphs) the size attribute is missing).

It seems that R copies graphs, and you are working on the copy in the loop, and the original objects are unaffected. Again, this is not specific to igraph, it is an R thing:

vectors <- list(1:5, 1:10)
for (v in vectors) length(v) <- 20
vectors
# [[1]]
# [1] 1 2 3 4 5
# [[2]]
#  [1]  1  2  3  4  5  6  7  8  9 10

for (i in 1:length(vectors)) length(vectors[[i]]) <- 20
vectors
# [[1]]
#  [1]  1  2  3  4  5 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
# [[2]]
#  [1]  1  2  3  4  5  6  7  8  9 10 NA NA NA NA NA NA NA NA NA NA

[...]

my config :
igraph 0.6-3
R 2.15.1

Btw. consider upgrading to version 0.6.5, because we fixed many bugs and as the release notes say:

"Many basic graph operations (eg. printing the summary to the screen, querying attribute values) now avoid copying the graph, so these operations are now much faster in R."

Best,
Gabor


reply via email to

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