Learn R Programming

igraph (version 1.2.11)

igraph-vs-indexing: Indexing vertex sequences

Description

Vertex sequences can be indexed very much like a plain numeric R vector, with some extras.

Usage

# S3 method for igraph.vs
[(x, ..., na_ok = FALSE)

Arguments

x

A vertex sequence.

...

Indices, see details below.

na_ok

Whether it is OK to have NAs in the vertex sequence.

Value

Another vertex sequence, referring to the same graph.

Multiple indices

When using multiple indices within the bracket, all of them are evaluated independently, and then the results are concatenated using the c() function (except for the na_ok argument, which is special an must be named. E.g. V(g)[1, 2, .nei(1)] is equivalent to c(V(g)[1], V(g)[2], V(g)[.nei(1)]).

Index types

Vertex sequences can be indexed with positive numeric vectors, negative numeric vectors, logical vectors, character vectors:

  • When indexed with positive numeric vectors, the vertices at the given positions in the sequence are selected. This is the same as indexing a regular R atomic vector with positive numeric vectors.

  • When indexed with negative numeric vectors, the vertices at the given positions in the sequence are omitted. Again, this is the same as indexing a regular R atomic vector.

  • When indexed with a logical vector, the lengths of the vertex sequence and the index must match, and the vertices for which the index is TRUE are selected.

  • Named graphs can be indexed with character vectors, to select vertices with the given names.

Vertex attributes

When indexing vertex sequences, vertex attributes can be referred to simply by using their names. E.g. if a graph has a name vertex attribute, then V(g)[name == "foo"] is equivalent to V(g)[V(g)$name == "foo"]. See examples below.

Special functions

There are some special igraph functions that can be used only in expressions indexing vertex sequences:

.nei

takes a vertex sequence as its argument and selects neighbors of these vertices. An optional mode argument can be used to select successors (mode="out"), or predecessors (mode="in") in directed graphs.

.inc

Takes an edge sequence as an argument, and selects vertices that have at least one incident edge in this edge sequence.

.from

Similar to .inc, but only considers the tails of the edges.

.to

Similar to .inc, but only considers the heads of the edges.

.innei, .outnei

.innei(v) is a shorthand for .nei(v, mode = "in"), and .outnei(v) is a shorthand for .nei(v, mode = "out").

Note that multiple special functions can be used together, or with regular indices, and then their results are concatenated. See more examples below.

Details

Vertex sequences can be indexed using both the single bracket and the double bracket operators, and they both work the same way. The only difference between them is that the double bracket operator marks the result for printing vertex attributes.

See Also

Other vertex and edge sequences: E(), V(), igraph-es-attributes, igraph-es-indexing2, igraph-es-indexing, igraph-vs-attributes, igraph-vs-indexing2, print.igraph.es(), print.igraph.vs()

Other vertex and edge sequence operations: 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()

Examples

Run this code
# NOT RUN {
# -----------------------------------------------------------------
# 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" ) ]
# }

Run the code above in your browser using DataLab