我有一个图形对象,g
并想在1.5中创建g_sub
一组顶点的邻域的子图。v_matches
g
order
换句话说,当且仅当这些顶点也连接到顶点时,我想找到哪些顶点连接到v_matches
(顺序 1),哪些顶点连接到 order==1顶点。v_matches
在非技术术语中,我想找到“朋友的朋友(订单 2),前提是这些朋友也是我的朋友(订单 1.5)”。我找不到在igraph
.
我编写的代码计算 2 的邻域v_matches
,但我希望它计算 1.5 的顺序。很感谢任何形式的帮助。
v_matches <- which(V(g)$SomeVertexAttribute==SomeValue)
g_sub <- induced.subgraph(graph=g,vids=unlist(neighborhood(g,order=2,nodes=v_matches)))
更新:这是我编写的一个函数和一些代码,用于将order=2
子图转换为order 1.5
. 但是,我不相信它工作正常。
Order2ToOrder1.5 <- function(g_sub,categoryValue) {
# get the new indexes of the 'ego' sites for the subgraph
matches_subgraph_gsub <- which(V(g_sub)$`Categorical 1`==categoryValue)
# get the edge list of the subgraph
edgelistTemp <- get.edgelist(g_sub)
# get unique "from" values
uniqueFrom <- unique(edgelistTemp[,1])
badVerts <- c() # yes, I know it is bad form to append to a vector using a for loop...
for (i in 1:length(uniqueFrom)){
# we find the immediate neighbours of each vertice
tempGraph <- as.vector(neighborhood(graph=g_sub,order=1,nodes=uniqueFrom[i])[[1]])
# if the neighbourhood does not contain one of the 'ego' sites (i.e. in matches_subgraph_gsub), then we flag this vertex for deletion
matchTemp <- match(tempGraph,matches_subgraph_gsub)
if (length(which(is.na(matchTemp)))==length(matchTemp)) {badVerts <- append(badVerts,uniqueFrom[i])}
}
# now we can delete these from the subgraph:
g_return <- delete.vertices(g_sub,badVerts)
}
categoryValue <- 21
matches <- which(V(g)$`Categorical 1`==categoryValue)
g_sub <- induced.subgraph(graph=g,vids=unlist(neighborhood(graph=g,order=2,nodes=matches)))
# We can use the new function to convert from order2 to order1.5:
g_sub_1.5 <- Order2ToOrder1.5(g_sub,categoryValue)