[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [igraph] Sorting a graph or assigning attributes based on vertex nam
From: |
Alan Labouseur |
Subject: |
Re: [igraph] Sorting a graph or assigning attributes based on vertex names |
Date: |
Fri, 2 Nov 2012 12:52:49 -0400 |
> V(g)$bucket[sortedVertexIDs] <- bv
... did indeed work, once I as.integer'd the sortedVertexIDs.
Thanks Tamás, that was a great help.
-Alan
> On Tue, Oct 30, 2012 at 6:18 PM, Tamás Nepusz <address@hidden> wrote:
> > Hi,
> >
> > First I would try replacing set.vertex.attribute(g, "bucket", vid, bv[i])
> > with:
> >
> > V(g)$bucket[vid] <- bv[i]
> >
> > I think this avoids copying the graph. Also, I would even try this (not
> > sure
> > if it works, but it's worth a try):
>
> Just to avoid mistakes in the archives. This also copies the graph. It
> is stupid, but it does. It is very hard to modify objects in place in
> R.
>
> > V(g)$bucket[sortedVertexIDs] <- bv
>
> This might work, but only if your vertex names are numbers from one to
> number-of-vertices, I believe.
>
> Gabor
>
> [...]
> On Oct 29, 2012, at 2:45 PM, Alan Labouseur <address@hidden> wrote:
>
>> Hi. I'm trying to assign a "bucket" attribute (from a vector of buckets) to
>> each vertex based on the position of the vertex degree when sorted. My
>> initial approach seems to work nicely up to the point of setting all of the
>> vertex attributes in the graph. After replicating the bucket vector so that
>> it's long enough to cover all of the vertices in g, I preserve g's vertex
>> IDs in the "name" attribute. Then I sort the vertices based on their degree.
>> Then I take only the "names" portion of the sorted vector which represents
>> the vertex IDs in the order I want to match them up to the buckets vector
>> (bv). But from that point it's horribly slow because of that for loop and
>> constantly reassigning the entire graph (g) after each set.vertex.attribute.
>> I bet there's a better way. I appreciate any help or ideas. Thanks. Here's
>> the code:
>>
>> foo <- function(g, buckets) {
>> # REPlicate buckets so that it's long enough to cover all the vertices in
>> g.
>> bv <- rep(buckets, (vcount(g) %/% length(buckets))+1)
>> #
>> # Label each vertex with its current id.
>> V(g)$name <- V(g) #
>> V(g)[1:20]$name
>> # Sort on degree.
>> sortedDegrees <- sort(degree(g), decreasing=TRUE) #
>> sortedDegrees[1:20]
>> # Get the vertices in degree-sorted order.
>> sortedVertexIDs <- attr(sortedDegrees, "names", exact=TRUE) #
>> sortedVertexIDs[1:20]
>> #
>> # Set the "bucket" attribute of every (sorted) vertex in g to the
>> corresponding (replicated) value in the bv.
>> i <- 1
>> for (vid in sortedVertexIDs) {
>> g <- set.vertex.attribute(g, "bucket", vid, bv[i])
>> i <- i + 1
>> }
>> #
>> # Return the entire graph.
>> g
>> }
>>
>> Thanks!
>> -Alan
>>
>
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Re: [igraph] Sorting a graph or assigning attributes based on vertex names,
Alan Labouseur <=