0

I have the following data

library(data.table); library(igraph)
t <- data.table(a=seq(ISOdate(2019,1,1), ISOdate(2019,7,1), "months"), 
                b=seq(ISOdate(2019,1,2), ISOdate(2019,7,2), "months"))
g <- graph_from_edgelist(as.matrix(t[,c("a","b")]))

and would like to apply subcomponent(g,ISOdate(2019,1,1),"out") but obtain the error that

 At structural_properties.c:1249 : subcomponent failed, Invalid vertex id

Is anyone aware of a solution to this problem?

Additional complication

The problem is complicated when I have an additional variable

start <- seq(ISOdate(2019,1,1), ISOdate(2019,7,1), "months")[c(1,3,5,7)]

that contains different starting vertices. Again, subcomponent(g,start,"out") gives the error above. Is there a workaround, similar to Ben's suggestion in the comments for the case above?

bumblebee
  • 1,116
  • 8
  • 20
  • 1
    Your `v` argument needs to be specified using the vertex ID not the name. Try: `subcomponent(g, V(g)$name == ISOdate(2019,1,1),"out")` – Ben Nutzer Jul 11 '19 at 07:48
  • Ah, my mistake! But what happens when I use a vector for `v`? I have added an edit above. – bumblebee Jul 11 '19 at 09:34

1 Answers1

0

Generally you could use the match function or the %in% operator. However, the subcomponent-function only allows for single queries. In order for this to work, I needed to adapt your start vector, because the timezone was added there but not in the original graph:

start <- gsub(" GMT", "", start)

So to answer your question (building on my comment from above): You could use the adjacent_vertices function, which queries multiple vertices at once.

adjacent_vertices(g, V(g)$name %in% start, mode = "out")

The drawback here is, that only the target vertices are being reported and not the start vertices. I am unaware of an existing function that corrects this. So in order to get both start and target vertices, you could write your own function:

lapply(1:length(start), function(x) subcomponent(g, V(g)$name == start[x], "out"))

I hope this helps.

Ben Nutzer
  • 1,082
  • 7
  • 15
  • I did some trial & error and found that `adjacent_vertices` seems impractical for my purposes because I need it to return more distant vertices as well. But of course, this wasn't part of the question. `subcomponent(.)` may still work because the function is nested within a data.table grouping and therefore needs to query only one vertex at a time. But somehow, it returns an empty vector. Feel free to take a look at https://stackoverflow.com/questions/57003667/igraph-posix-and-data-table where I describe the problem in more detail. – bumblebee Jul 12 '19 at 09:03