I have a problem writing my data frame to csv file.
g looks like this
> g
IGRAPH c32bbbf UN-- 12 12 --
+ attr: name (v/c)
+ edges from c32bbbf (vertex names):
[1] 2 --3 3 --1 4 --5 5 --6 4 --6 6 --7 6 --8 6 --9 9 --8 10--11 11--12 10--12
None of folowing code works
write.table(g, file = "filename.csv", sep = ";", dec = ",", row.names = FALSE)
write.csv2(membership(cluster_edge_betweenness(g)), "name.csv" , row.names = FALSE)
write.table(membership(cluster_edge_betweenness(g)), file = "name.csv", sep = ";", dec = ",", row.names = FALSE)
It shows this error
Error in as.data.frame.default(x[[i]], optional = TRUE) :
cannot coerce class ‘"membership"’ to a data.frame
How can I write g or membership(cluster_edge_betweenness(g)) into a.csv file? Thanks for help in advance.
Use igraph's write_graph()
function. Check the documentation for available formats.
write.table
and write.csv
require that the input be a tabular data (data.frame/matrix/etc.). Your object g
is an igraph object. As others have mentioned, one option is to use igraph's write_graph()
function , that will write the graph to a text file and allows different formats for how the data is written.
Another option would be to use igraph's as_long_data_frame
function to convert your graph object to a dataframe, which can then be written to a csv file using write.csv
. However, this requires that the vertices be named.
For example:
library(igraph)
g <- make_ring(10)
# add names to vertices
vertex_attr(g) <- list(name = LETTERS[1:10])
vertex_attr(g, "label") <- V(g)$name
as_long_data_frame(g)
#> from to from_name from_label to_name to_label
#> 1 2 A A B B
#> 2 3 B B C C
#> 3 4 C C D D
#> 4 5 D D E E
#> 5 6 E E F F
#> 6 7 F F G G
#> 7 8 G G H H
#> 8 9 H H I I
#> 9 10 I I J J
#> 1 10 A A J J
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.