[英]'names' attribute [1] must be the same length as the vector [0]
我正在將鄰接矩陣轉換為圖形對象到數據框。 我正在嘗試以下代碼:
df.h <- graph_from_adjacency_matrix(X_final,mode = "directed")
test2 <- as_long_data_frame(graph = df.h)
但是,我收到錯誤:
Error : Error in names(ver) <- paste0("from_", names(ver)) : 'names' attribute [1] must be the same length as the vector [0]
如何解決問題?
為了讓這個函數工作,你需要指定一個name
屬性,例如圖形的 id:
set.seed(1)
g <- erdos.renyi.game(10,.2)
V(g)$name <- V(g)
然后函數應該運行:
> head(as_long_data_frame(g), 5)
from to ver[el[, 1], ] ver2[el[, 2], ]
1 2 3 2 3
2 3 4 3 4
3 4 7 4 7
4 5 7 5 7
5 5 8 5 8
但是,我認為這是一個很好的發現:截至目前(2019 年 9 月, v1.2.4.1
),整體功能似乎尚未完成(請參閱上面的隱晦列名)。 一旦我按照文檔?as_long_data_frame
建議添加了第二個屬性,該函數就會(完全)按預期工作:
V(g)$color <- colors()[1:10]
> head(as_long_data_frame(g), 5)
from to from_name from_color to_name to_color
1 2 3 2 aliceblue 3 antiquewhite
2 3 4 3 antiquewhite 4 antiquewhite1
3 4 7 4 antiquewhite1 7 antiquewhite4
4 5 7 5 antiquewhite2 7 antiquewhite4
5 5 8 5 antiquewhite2 8 aquamarine
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.