问题
Building off this question here, is there a way to extend this subgraph to include vertices connected by two degrees to a subset of vertices? I'm thinking of a command similar to the functions in make_ego_graph() where order=2 and mode="in". I'm working with a directed graph object.
Thus far, I've come up with the following, but it's not producing the graph I'm looking for.
first_degree <- V(graph)$condition == "something"
second_degree <- V(graph)[to(first_degree)]
edges_subset <- E(graph)[to(first_degree) | to(second_degree)]
desired_subset <- subgraph.edges(graph, edges_subset)
Thanks for any advice you can give me!
回答1:
It's not the most elegant solution, but it seems to work.
ego.list <- make_ego_graph(graph, order=2, nodes=V(graph)$condition=="something")
desired_subset <- NULL
for (i in seq_along(ego.list)){
x <- ego.list[[i]]
desired_subset <- graph.union(desired_subset, x)
}
来源:https://stackoverflow.com/questions/44712041/subset-igraph-object-to-just-2nd-order-ego-graph-of-certain-vertices