问题描述
我有以下数据
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")]))
,并希望应用subcomponent(g,ISOdate(2019,1,1),"out")
,但会获得错误
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?
其他并发症
当我有一个附加变量时,问题很复杂
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)]
包含不同的起始顶点.同样,subcomponent(g,start,"out")
给出上面的错误.是否有一种变通方法,类似于本案评论中Ben的建议?
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?
推荐答案
通常,您可以使用match
函数或%in%
运算符.但是,subcomponent
函数仅允许单个查询.为了使它起作用,我需要调整您的start
向量,因为时区已添加到此处,但未添加到原始图中:
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)
所以要回答您的问题(基于我在上面的评论):您可以使用adjacent_vertices
函数,该函数一次查询多个顶点.
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"))
我希望这会有所帮助.
这篇关于笔画POSIXct的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!