vendredi 28 août 2015

How to randomly select 2 vertices from a graph in R?

I'm new to R, and I'm trying to randomly select 2 vertices from a graph.

What I've done so far is: First, set up a graph

edgePath <- "./project1/data/smalledges.csv"

edgesMatrix <- as.matrix(read.csv(edgePath, header = TRUE, colClasses = "character"))
graph <- graph.edgelist(edgesMatrix)

The smalledges.csv is a file look like this:

from     to
4327231  2587908

Then I get all the vertices from the graph into a list:

vList <- as.list(get.data.frame(graph, what = c("vertices")))

After that, I try to use:

sample(vList, 2)

But what I've got is an error:

cannot take a sample larger than the population when 'replace = FALSE'

I guess it's because R thinks what I want is 2 random lists, so I tried this:

sample(vList, 2, replace = TRUE)

And then I've got 2 large lists... BUT THAT'S NOT WHAT I WANTED! So guys, how can I randomly select 2 vertices from my graph? Thanks!




Aucun commentaire:

Enregistrer un commentaire