igraph (version 1.0.0)

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 class '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 isTRUEare 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 refered 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.

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 sequence operations: [.igraph.es, %--%, %->%, %<-%, igraph-es-indexing; [[.igraph.es, igraph-es-indexing2; [[.igraph.vs, igraph-vs-indexing2; c.igraph.es; c.igraph.vs; difference.igraph.es; difference.igraph.vs; intersection.igraph.es; intersection.igraph.vs; rev.igraph.es; rev.igraph.vs; union.igraph.es; union.igraph.vs; unique.igraph.es; unique.igraph.vs

Other vertex and edge sequences: $.igraph.es, $<-.igraph.es, E<-, [<-.igraph.es, [[<-.igraph.es, igraph-es-attributes, igraph-es-attributes, igraph-es-attributes, igraph-es-attributes, igraph-es-attributes; $.igraph.vs, $<-.igraph.vs, V<-, [<-.igraph.vs, [[<-.igraph.vs, igraph-vs-attributes, igraph-vs-attributes, igraph-vs-attributes, igraph-vs-attributes, igraph-vs-attributes; E; V; [.igraph.es, %--%, %->%, %<-%, igraph-es-indexing; [[.igraph.es, igraph-es-indexing2; [[.igraph.vs, igraph-vs-indexing2; print.igraph.es; print.igraph.vs

Examples

Run this code
# -----------------------------------------------------------------
# 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 DataCamp Workspace