簡體   English   中英

如何保存 igraph object

[英]How to save an igraph object

我在將數據框寫入 csv 文件時遇到問題。

g 看起來像這樣

> 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

以下代碼均無效

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)

它顯示了這個錯誤

Error in as.data.frame.default(x[[i]], optional = TRUE) : 
  cannot coerce class ‘"membership"’ to a data.frame

如何將 g 或 members(cluster_edge_betweenness(g)) 寫入 a.csv 文件? 提前感謝您的幫助。

使用 igraph 的write_graph() function。 檢查可用格式的文檔。

write.tablewrite.csv要求輸入是表格數據(data.frame/matrix/etc.)。 您的 object g是 igraph object。 正如其他人所提到的,一種選擇是使用 igraph 的write_graph() function ,它將圖形寫入文本文件並允許不同格式的數據寫入方式。

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 . 但是,這需要命名頂點。

例如:

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  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM