Question

I have a membership vector created with another software and I am stuck to write it into R so that I can use iGraph' modularity function to calculate modularity of this community division. 

Can someone help me with how to write the vector into R so that the Modularity(g,membership) could run?

I tried with using membership <- read.table(file), but the result could not be used with Modularity(g, membership)

Thanks,

Song

Was it helpful?

Solution

read.table creates a data frame, you need to convert that to a simple numeric vector. Alternatively you can use scan(). You might need to adjust the following to your data format.

library(igraph)
G <- graph.full(3) + graph.ring(3) + graph.full(3)

contents <- '1 1 1 2 2 2 3 3 3'
memb <- scan(textConnection(contents))
# Read 9 items

modularity(G, memb)
# [1] 0.6666667

Instead of the textConnection(), just put your file name there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top