'names' 属性 [1] 的长度必须与向量 [0] 的长度相同

'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