EVOLUTION-MANAGER
Edit File: igraph-vs-indexing.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Indexing vertex sequences</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for igraph-vs-indexing {igraph}"><tr><td>igraph-vs-indexing {igraph}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Indexing vertex sequences</h2> <h3>Description</h3> <p>Vertex sequences can be indexed very much like a plain numeric R vector, with some extras. </p> <h3>Usage</h3> <pre> ## S3 method for class 'igraph.vs' x[..., na_ok = FALSE] </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>A vertex sequence.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>Indices, see details below.</p> </td></tr> <tr valign="top"><td><code>na_ok</code></td> <td> <p>Whether it is OK to have <code>NA</code>s in the vertex sequence.</p> </td></tr> </table> <h3>Details</h3> <p>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. </p> <h3>Value</h3> <p>Another vertex sequence, referring to the same graph. </p> <h3>Multiple indices</h3> <p>When using multiple indices within the bracket, all of them are evaluated independently, and then the results are concatenated using the <code>c()</code> function (except for the <code>na_ok</code> argument, which is special an must be named. E.g. <code>V(g)[1, 2, .nei(1)]</code> is equivalent to <code>c(V(g)[1], V(g)[2], V(g)[.nei(1)])</code>. </p> <h3>Index types</h3> <p>Vertex sequences can be indexed with positive numeric vectors, negative numeric vectors, logical vectors, character vectors: </p> <ul> <li><p> 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. </p> </li> <li><p> 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. </p> </li> <li><p> 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 <code>TRUE</code> are selected. </p> </li> <li><p> Named graphs can be indexed with character vectors, to select vertices with the given names. </p> </li></ul> <h3>Vertex attributes</h3> <p>When indexing vertex sequences, vertex attributes can be referred to simply by using their names. E.g. if a graph has a <code>name</code> vertex attribute, then <code>V(g)[name == "foo"]</code> is equivalent to <code>V(g)[V(g)$name == "foo"]</code>. See more examples below. Note that attribute names mask the names of variables present in the calling environment; if you need to look up a variable and you do not want a similarly named vertex attribute to mask it, use the <code>.env</code> pronoun to perform the name lookup in the calling environment. In other words, use <code>V(g)[.env$name == "foo"]</code> to make sure that <code>name</code> is looked up from the calling environment even if there is a vertex attribute with the same name. Similarly, you can use <code>.data</code> to match attribute names only. </p> <h3>Special functions</h3> <p>There are some special igraph functions that can be used only in expressions indexing vertex sequences: </p> <dl> <dt><code>.nei</code></dt><dd><p>takes a vertex sequence as its argument and selects neighbors of these vertices. An optional <code>mode</code> argument can be used to select successors (<code>mode="out"</code>), or predecessors (<code>mode="in"</code>) in directed graphs.</p> </dd> <dt><code>.inc</code></dt><dd><p>Takes an edge sequence as an argument, and selects vertices that have at least one incident edge in this edge sequence.</p> </dd> <dt><code>.from</code></dt><dd><p>Similar to <code>.inc</code>, but only considers the tails of the edges.</p> </dd> <dt><code>.to</code></dt><dd><p>Similar to <code>.inc</code>, but only considers the heads of the edges.</p> </dd> <dt><code>.innei</code>, <code>.outnei</code></dt><dd><p><code>.innei(v)</code> is a shorthand for <code>.nei(v, mode = "in")</code>, and <code>.outnei(v)</code> is a shorthand for <code>.nei(v, mode = "out")</code>. </p> </dd> </dl> <p>Note that multiple special functions can be used together, or with regular indices, and then their results are concatenated. See more examples below. </p> <h3>See Also</h3> <p>Other vertex and edge sequences: <code><a href="E.html">E</a>()</code>, <code><a href="V.html">V</a>()</code>, <code><a href="igraph-es-attributes.html">igraph-es-attributes</a></code>, <code><a href="igraph-es-indexing2.html">igraph-es-indexing2</a></code>, <code><a href="igraph-es-indexing.html">igraph-es-indexing</a></code>, <code><a href="igraph-vs-attributes.html">igraph-vs-attributes</a></code>, <code><a href="igraph-vs-indexing2.html">igraph-vs-indexing2</a></code>, <code><a href="print.igraph.es.html">print.igraph.es</a>()</code>, <code><a href="print.igraph.vs.html">print.igraph.vs</a>()</code> </p> <p>Other vertex and edge sequence operations: <code><a href="c.igraph.es.html">c.igraph.es</a>()</code>, <code><a href="c.igraph.vs.html">c.igraph.vs</a>()</code>, <code><a href="difference.igraph.es.html">difference.igraph.es</a>()</code>, <code><a href="difference.igraph.vs.html">difference.igraph.vs</a>()</code>, <code><a href="igraph-es-indexing2.html">igraph-es-indexing2</a></code>, <code><a href="igraph-es-indexing.html">igraph-es-indexing</a></code>, <code><a href="igraph-vs-indexing2.html">igraph-vs-indexing2</a></code>, <code><a href="intersection.igraph.es.html">intersection.igraph.es</a>()</code>, <code><a href="intersection.igraph.vs.html">intersection.igraph.vs</a>()</code>, <code><a href="rev.igraph.es.html">rev.igraph.es</a>()</code>, <code><a href="rev.igraph.vs.html">rev.igraph.vs</a>()</code>, <code><a href="union.igraph.es.html">union.igraph.es</a>()</code>, <code><a href="union.igraph.vs.html">union.igraph.vs</a>()</code>, <code><a href="unique.igraph.es.html">unique.igraph.es</a>()</code>, <code><a href="unique.igraph.vs.html">unique.igraph.vs</a>()</code> </p> <h3>Examples</h3> <pre> # ----------------------------------------------------------------- # 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] </pre> <hr /><div style="text-align: center;">[Package <em>igraph</em> version 1.3.5 <a href="00Index.html">Index</a>]</div> </body></html>