[R] matrix into vector with vertex names

Gábor Csárdi csardi at rmki.kfki.hu
Thu Aug 18 12:47:56 CEST 2011


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