Search code examples
rposixigraph

numeric POSIX objects in igraph


I have a matrix of POSIXct objects

library(data.table)
p <- data.table(a=seq(from=ISOdate(2019,1,1,12,0), to=ISOdate(2019,1,2,0,0), by="hours"), b=seq(from=ISOdate(2019,2,1,12,0), to=ISOdate(2019,2,2,0,0), by="hours"))

and I would like to apply graph_from_edgelist(.) to it. I have tried it directly

library(igraph)
g <- graph_from_edgelist(as.matrix(p[,c("a","b")]))

and this works quickly and without problems. However, doing it this way causes problems down the line (described here), so I tried using the numeric transformation of the POSIX objects:

p$na <- as.numeric(p$a); p$nb <- as.numeric(p$b)
g <- graph_from_edgelist(as.matrix(p[,c("na","nb")]))

This, however, crashes my machine, saying that R requires enormous RAM. Why is this so?


Solution

  • ?graph_from_edgelist mentions that if a numeric matrix is passed then the values are treated as vertex ids. The following example illustrates:

    > g <- graph_from_edgelist(matrix(c(1,10), ncol=2))
    > E(g)
    + 1/1 edge from 968b115:
    [1] 1->10
    > V(g)
    + 10/10 vertices, from 968b115:
     [1]  1  2  3  4  5  6  7  8  9 10
    

    While there is only one edge, due to the vertex ids that are passed it is assumed that there are 10 vertices. You can see this from the code of graph_from_edgelist which calls graph for numeric matrices -- it has form graph(edges, n=max(edges), ...) so the number of vertices that will be added to the graph is equal to the largest id that you pass.

    As your example has large numbers as the vertex id's the graph command is adding a huge bunch of unwanted vertices, hence memory issue. Depending on what you want, you could convert the edges with p$na <- as.character(as.numeric(...)), or p$na <- as.character(p$a) etc and then use graph_from_edgelist or just use graph_from_data_frame directly.