问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/56983943/igraph-posixct