R igraph 手册页

如果您从 R 中使用 igraph,请使用此选项

igraph-vs-indexing {igraph}R 文档

索引顶点序列

描述

顶点序列可以像普通的 R 数值向量一样进行索引,但有一些额外的功能。

用法

## S3 method for class 'igraph.vs'
x[..., na_ok = FALSE]

参数

x

一个顶点序列。

...

索引,详见下文。

na_ok

是否允许顶点序列中存在 NA 值。

详细信息

顶点序列可以使用单括号和双括号运算符进行索引,并且它们的工作方式相同。它们之间唯一的区别是双括号运算符会将结果标记为打印顶点属性。

另一个顶点序列,引用相同的图。

多个索引

当在括号中使用多个索引时,所有索引都会被独立评估,然后使用 c() 函数将结果连接起来(除了 na_ok 参数,它比较特殊,必须被命名)。例如,V(g)[1, 2, .nei(1)] 等价于 c(V(g)[1], V(g)[2], V(g)[.nei(1)])

索引类型

顶点序列可以使用正数值向量、负数值向量、逻辑向量、字符向量进行索引

  • 当使用正数值向量进行索引时,会选择序列中给定位置的顶点。这与使用正数值向量索引常规 R 原子向量相同。

  • 当使用负数值向量进行索引时,会省略序列中给定位置的顶点。同样,这与索引常规 R 原子向量相同。

  • 当使用逻辑向量进行索引时,顶点序列和索引的长度必须匹配,并且会选择索引为 TRUE 的顶点。

  • 命名图可以使用字符向量进行索引,以选择具有给定名称的顶点。

顶点属性

索引顶点序列时,可以通过简单地使用属性名称来引用顶点属性。例如,如果一个图有一个 name 顶点属性,那么 V(g)[name == "foo"] 等价于 V(g)[V(g)$name == "foo"]。请参见下面的更多示例。请注意,属性名称会屏蔽调用环境中存在的变量的名称;如果您需要查找一个变量并且不希望具有类似名称的顶点属性屏蔽它,请使用 .env 代词在调用环境中执行名称查找。换句话说,使用 V(g)[.env$name == "foo"] 以确保从调用环境中查找 name,即使存在具有相同名称的顶点属性。类似地,您可以使用 .data 仅匹配属性名称。

特殊函数

有一些特殊的 igraph 函数只能在索引顶点序列的表达式中使用

.nei

以顶点序列作为其参数,并选择这些顶点的邻居。可选的 mode 参数可用于在有向图中选择后继顶点(mode="out")或前驱顶点(mode="in")。

.inc

以边序列作为参数,并选择在该边序列中至少有一条关联边的顶点。

.from

类似于 .inc,但只考虑边的尾部。

.to

类似于 .inc,但只考虑边的头部。

.innei, .outnei

.innei(v).nei(v, mode = "in") 的简写形式,.outnei(v).nei(v, mode = "out") 的简写形式。

请注意,多个特殊函数可以一起使用,或者与常规索引一起使用,然后将它们的结果连接起来。请参见下面的更多示例。

参见

其他顶点和边序列: E(), V(), igraph-es-attributes, igraph-es-indexing2, igraph-es-indexing, igraph-vs-attributes, igraph-vs-indexing2, print.igraph.es(), print.igraph.vs()

其他顶点和边序列操作: c.igraph.es(), c.igraph.vs(), difference.igraph.es(), difference.igraph.vs(), igraph-es-indexing2, igraph-es-indexing, igraph-vs-indexing2, intersection.igraph.es(), intersection.igraph.vs(), rev.igraph.es(), rev.igraph.vs(), union.igraph.es(), union.igraph.vs(), unique.igraph.es(), unique.igraph.vs()

示例

# -----------------------------------------------------------------
# Setting attributes for subsets of vertices
largest_comp <- function(graph) {
  cl <- components(graph)
  V(graph)[which.max(cl$csize) == cl$membership]
}
g <- sample_(gnp(100, 2/100),
  with_vertex_(size = 3, label = ""),
  with_graph_(layout = layout_with_fr)
)
giant_v <- largest_comp(g)
V(g)$color <- "green"
V(g)[giant_v]$color <- "red"
plot(g)

# -----------------------------------------------------------------
# nei() special function
g <- graph( c(1,2, 2,3, 2,4, 4,2) )
V(g)[ .nei( c(2,4) ) ]
V(g)[ .nei( c(2,4), "in") ]
V(g)[ .nei( c(2,4), "out") ]

# -----------------------------------------------------------------
# The same with vertex names
g <- graph(~ A -+ B, B -+ C:D, D -+ B)
V(g)[ .nei( c('B', 'D') ) ]
V(g)[ .nei( c('B', 'D'), "in" ) ]
V(g)[ .nei( c('B', 'D'), "out" ) ]

# -----------------------------------------------------------------
# Resolving attributes
g <- graph(~ A -+ B, B -+ C:D, D -+ B)
V(g)$color <- c("red", "red", "green", "green")
V(g)[ color == "red" ]

# Indexing with a variable whose name matches the name of an attribute
# may fail; use .env to force the name lookup in the parent environment
V(g)$x <- 10:13
x <- 2
V(g)[.env$x]


[包 igraph 版本 1.3.5 索引]