Search code examples
rdataframematrixigraphadjacency-matrix

'names' attribute [1] must be the same length as the vector [0]


I am converting an adjacency matrix to graph object to data frame. I am trying the following code :

df.h <- graph_from_adjacency_matrix(X_final,mode = "directed")
test2 <- as_long_data_frame(graph = df.h)

But, I am getting the error:

Error : Error in names(ver) <- paste0("from_", names(ver)) :    'names' attribute [1] must be the same length as the vector [0]

How to resolve the issue?


Solution

  • In order for this function to work you need to specify a name attribute, e.g. the id of the graph:

    set.seed(1)
    g <- erdos.renyi.game(10,.2)
    V(g)$name <- V(g)
    

    Then the function should run:

    > 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
    

    However, I think it's a good catch of you: As of now (September 2019, v1.2.4.1) the overall function seems to be unfinished (see the cryptic colnames above). Once I add a second attribute, as proposed by the documentation ?as_long_data_frame, the function works (fully) as intended:

    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