Omar
Omar

Reputation: 65

Igraph Write Communities

We are using igraph and R to detect communities in a network. The detection using cluster_walktrap is working great:

e <- cluster_walktrap(g)

com <-membership(e)

print(com)

write.csv2(com, file ="community.csv", sep=",")

The result is printed fine using print with the number and the community number that it belongs to but we have a problem in writing the result in the csv file and I have an error : cannot coerce class ""membership"" to a data.frame

How can I write the result of membership in a file ?

Thanks

Upvotes: 1

Views: 2768

Answers (2)

I don't know if you guys resolved the problem but I did the following using R:

```
# applying the community method
com = spinglass.community(graph_builted,
                        weights = graph_builted$weights,
                        implementation = "orig", 
                        update.rule = "config")

# creating a data frame to store the results
type = c(0)
labels = c(0)
groups = c(0)
res2 = data.frame(type, labels, groups)   

labels = com$names             # here you get the vertices names
groups = com$membership        # here you get the communities indices 

# here you save the information
res = data.frame(type = "spinGlass1", labels, groups)
res2 = rbind(res2, res) 

# then you save the .csv file 
write.csv(res2, "spinglass-communities.csv")

```

That resolves the problem for me. Best regards.

Upvotes: 1

paqmo
paqmo

Reputation: 3739

Convert the membership object to numeric. write.csv and write.csv2 expect a data frame or matrix. The command tries to coerce the object into a data frame, which the class membership resists. Since membership really is just a vector, you can convert it a numeric. Either:

write.csv2(as.numeric(com), file ="community.csv")

Or:

com <- as.numeric(com)
write.csv2(com, file ="community.csv")

Oh, and you don't need the sep = "," argument for write.csv.

If you want to create table of vertex names/numbers and groups:

com <- cbind(V(g),e$membership) #V(g) gets the number of vertices
com <- cbind(V(g)$name,e$membership) #To get names if your vertices are labeled

Upvotes: 3

Related Questions