[R] matrix into vector with vertex names

joe j joe.stata at gmail.com
Wed Aug 24 10:39:09 CEST 2011


Hi Gabor,

Thanks. I will try to figure out the solution you suggest. I found out
about melt() from a discussion forum; it seems to me that
"melt()$value" is similar to "c()", and when I modified the script as
below it 'seems' to be running faster. Anyway in the end I only needed
to use a smaller network so the computation went ok.

y_s <- melt(y_s)[c(upper.tri(y_s)),]  #new code

y_s <- melt(y_s)[melt(upper.tri(y_s))$value,] #old code

The reason I needed three columns was for later statistical analysis
for which such a format was necessary.

Best regards,
Joe.

On Thu, Aug 18, 2011 at 12:47 PM, Gábor Csárdi <csardi at rmki.kfki.hu> wrote:
> Joe,
>
> what is melt() supposed to do here?
>
> What's wrong with the simple solution of creating a data.frame first,
> and then filling it with values through a loop? Actually, keeping the
> matrix is just as good, indexing is just as fast, and takes the same
> amount of memory as your three column matrix, doesn't it?
>
> Gabor
>
> On Fri, Aug 5, 2011 at 10:40 AM, joe j <joe.stata at gmail.com> wrote:
>> Using Igraph, I create shortest paths, then convert the matrix into
>> three column vectors - "vertex1", "vertex2", "shortestpath" - as the
>> code below shows.
>>
>> #code for generating shortest path matrix and creating a 3 columns
>> from an igraph graph object "y"
>> y_s<-shortest.paths(y,  weights = NULL)
>> y_s <- melt(y_s)[melt(upper.tri(y_s))$value,] #Step 2: this is where
>> the trouble with memory occurs
>> y_s[,1] <- V(y)$name[y_s[,1]]
>> y_s[,2] <- V(y)$name[y_s[,2]]
>> names(y_s)<-c("vertex1", "vertex2", "shortestpath")
>>
>> However I am looking for an alternative way of doing this becase at
>> the second step I run into a fight with my machine's memory. I know I
>> can create vectors using as.vector(), c(), etc, but I am not able to
>> create the two other columns with vertex names.
>>
>> Best regards,
>> Joe.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
>
> --
> Gabor Csardi <csardi at rmki.kfki.hu>     MTA KFKI RMKI
>



More information about the R-help mailing list