简体   繁体   English

如何使用具有 NA 值的顶点属性对 igraph object(图)进行子图化 - R

[英]how to subgraph an igraph object (graph) using vertices attribute that has NA values - R

I'm trying to subgraph a graph using igraph in by filtering vertices based on the value of an attribute of the vertices.我正在尝试通过基于顶点属性值过滤顶点来使用 igraph in 子图。 the attribute values can be NA and I want those with NA values to be excluded.属性值可以是 NA ,我希望排除具有 NA 值的那些。

here is my graph这是我的图表

> require(igraph)
> graph <- make_ring(7)
> V(graph)$name <- c("A", "B", "C", "D", "E", "F", "G")
> V(graph)$att1 <- c(1,2,NA,1,2,3,NA)
> subgraph <- make_ego_graph(graph, order=1, c("A", "D", "F"))  # this works because i'm using the names of vertices but it's not what I want

all i'm trying to do here is to get the names of the vertices that have att1==1, and use that instead of to subgraph.我在这里要做的就是获取具有 att1==1 的顶点的名称,并使用它而不是子图。 But this doesn't work and gives me the following error但这不起作用并给我以下错误

 > V(graph)[att1 == 1, na_ok = TRUE]$name  

    Error in if (is.numeric(v) && any(v < 0)) { :    missing value where
        TRUE/FALSE needed

how do I get the vector of the names for vertices that have att1 == 1 to pass it to the subgraph function?如何获取具有 att1 == 1 的顶点的名称向量以将其传递给子图 function? or is there another way to subgraph using an attribute value?还是有另一种使用属性值子图的方法? I want the subgraph to include all the vertices selected (att1 == 1) AND the vertices that connect to those vertices.我希望子图包含所有选定的顶点(att1 == 1)以及连接到这些顶点的顶点。

Is this what you are looking for?这是你想要的?

require(igraph)
graph <- make_ring(7)
V(graph)$name <- c("A", "B", "C", "D", "E", "F", "G")
V(graph)$att1 <- c(1,2,NA,1,2,3,NA)

V(graph)$name[which(V(graph)$att1 == 1)] 
#> [1] "A" "D"

Lately I became a fan of the tidygraph package which is a wrapper around igraph , so graphs can be manipulated using dplyr and other tidyverse packages.最近我成为了tidygraph package 的粉丝,它是igraph的包装,因此可以使用dplyr和其他tidyverse包来操作图形。 In this case:在这种情况下:

library(tidygraph)
library(dplyr)

as_tbl_graph(graph) %>% 
  activate(nodes) %>% 
  filter(att1 == 1) %>% 
  pull(name)
#> [1] "A" "D"

Or directly subset the graph (afaik)或直接对图进行子集化(afaik)

graph_tidy <- as_tbl_graph(graph) %>% 
  activate(nodes) %>% 
  filter(att1 == 1)

graph_tidy
#> # A tbl_graph: 2 nodes and 0 edges
#> #
#> # An undirected simple graph with 2 components
#> #
#> # Node Data: 2 x 2 (active)
#>   name   att1
#>   <chr> <dbl>
#> 1 A         1
#> 2 D         1
#> #
#> # Edge Data: 0 x 2
#> # ... with 2 variables: from <int>, to <int>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R iGraph 删除属性值 = NA 的顶点 - R iGraph remove vertices with attribute value = NA R igraph 从顶点列表中的 igraph object 创建“子图”,如果原始节点中有连接的节点,则推断所选顶点之间的边 - R igraph make “subgraph” from igraph object from list of vertices, infer edges between selected vertices if there are connected nodes in original 在R中,如何根据多个属性分数从igraph对象生成子图? - In R, how can I generate a subgraph from a igraph object based on multiple attribute scores? 在R中使用igraph创建子图 - Creating Subgraph using igraph in R R-使用顶点属性igraph制作新的图形对象 - R - Make new graph object using vertex attribute igraph 如何在R中使用igraph从图中仅提取具有多个边的顶点 - how to extract only the vertices with multiple edges from a graph using igraph in R 使用R中的line.graph(igraph)将顶点转换为边缘时,如何计算新的边缘权重? - How to computer new edge weight when converting vertices to edges using line.graph (igraph) in R? 如何使用R中的igraph绘制链接到同一顶点的图形的所有顶点的子集? - How to plot a subset of all the vertices of a graph that are linked to same vertex using igraph in R? 使用线图计算子图的程度 - Calculate degree of a subgraph using r igraph R Igraph子图给出节点索引和要包含在图中的节点数 - R Igraph subgraph given node index and number of nodes to include in the graph
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM