1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/iterators.R
\name{igraph-es-indexing}
\alias{igraph-es-indexing}
\alias{[.igraph.es}
\alias{\%--\%}
\alias{\%<-\%}
\alias{\%->\%}
\title{Indexing edge sequences}
\usage{
\method{[}{igraph.es}(x, ...)
}
\arguments{
\item{x}{An edge sequence}
\item{...}{Indices, see details below.}
}
\value{
Another edge sequence, referring to the same graph.
}
\description{
Edge sequences can be indexed very much like a plain numeric R vector,
with some extras.
}
\section{Multiple indices}{
When using multiple indices within the bracket, all of them
are evaluated independently, and then the results are concatenated
using the \code{c()} function. E.g. \code{E(g)[1, 2, .inc(1)]}
is equivalent to \code{c(E(g)[1], E(g)[2], E(g)[.inc(1)])}.
}
\section{Index types}{
Edge sequences can be indexed with positive numeric vectors,
negative numeric vectors, logical vectors, character vectors:
\itemize{
\item When indexed with positive numeric vectors, the edges at the
given positions in the sequence are selected. This is the same as
indexing a regular R atomic vector with positive numeric vectors.
\item When indexed with negative numeric vectors, the edges at the
given positions in the sequence are omitted. Again, this is the same
as indexing a regular R atomic vector.
\item When indexed with a logical vector, the lengths of the edge
sequence and the index must match, and the edges for which the
index is \code{TRUE} are selected.
\item Named graphs can be indexed with character vectors,
to select edges with the given names. Note that a graph may
have edge names and vertex names, and both can be used to select
edges. Edge names are simply used as names of the numeric
edge id vector. Vertex names effectively only work in graphs without
multiple edges, and must be separated with a \code{|} bar character
to select an edges that incident to the two given vertices. See
examples below.
}
}
\section{Edge attributes}{
When indexing edge sequences, edge attributes can be referred
to simply by using their names. E.g. if a graph has a \code{weight} edge
attribute, then \code{E(G)[weight > 1]} selects all edges with a weight
larger than one. 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 edge attribute to mask it,
use the \code{.env} pronoun to perform the name lookup in the calling
environment. In other words, use \code{E(g)[.env$weight > 1]} to make sure
that \code{weight} is looked up from the calling environment even if there is
an edge attribute with the same name. Similarly, you can use \code{.data} to
match attribute names only.
}
\section{Special functions}{
There are some special igraph functions that can be used
only in expressions indexing edge sequences: \describe{
\item{\code{.inc}}{takes a vertex sequence, and selects
all edges that have at least one incident vertex in the vertex
sequence.}
\item{\code{.from}}{similar to \code{.inc()}, but only
the tails of the edges are considered.}
\item{\code{.to}}{is similar to \code{.inc()}, but only
the heads of the edges are considered.}
\item{\verb{\\\%--\\\%}}{a special operator that can be
used to select all edges between two sets of vertices. It ignores
the edge directions in directed graphs.}
\item{\verb{\\\%->\\\%}}{similar to \verb{\\\%--\\\%},
but edges \emph{from} the left hand side argument, pointing
\emph{to} the right hand side argument, are selected, in directed
graphs.}
\item{\verb{\\\%<-\\\%}}{similar to \verb{\\\%--\\\%},
but edges \emph{to} the left hand side argument, pointing
\emph{from} the right hand side argument, are selected, in directed
graphs.}
}
Note that multiple special functions can be used together, or with
regular indices, and then their results are concatenated. See more
examples below.
}
\examples{
# -----------------------------------------------------------------
# Special operators for indexing based on graph structure
g <- sample_pa(100, power = 0.3)
E(g)[1:3 \%--\% 2:6]
E(g)[1:5 \%->\% 1:6]
E(g)[1:3 \%<-\% 2:6]
# -----------------------------------------------------------------
# The edges along the diameter
g <- sample_pa(100, directed = FALSE)
d <- get_diameter(g)
E(g, path = d)
# -----------------------------------------------------------------
# Select edges based on attributes
g <- sample_gnp(20, 3 / 20) \%>\%
set_edge_attr("weight", value = rnorm(gsize(.)))
E(g)[[weight < 0]]
# 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
E(g)$x <- E(g)$weight
x <- 2
E(g)[.env$x]
}
\seealso{
Other vertex and edge sequences:
\code{\link{E}()},
\code{\link{V}()},
\code{\link{as_ids}()},
\code{\link{igraph-es-attributes}},
\code{\link{igraph-es-indexing2}},
\code{\link{igraph-vs-attributes}},
\code{\link{igraph-vs-indexing}},
\code{\link{igraph-vs-indexing2}},
\code{\link{print.igraph.es}()},
\code{\link{print.igraph.vs}()}
Other vertex and edge sequence operations:
\code{\link{c.igraph.es}()},
\code{\link{c.igraph.vs}()},
\code{\link{difference.igraph.es}()},
\code{\link{difference.igraph.vs}()},
\code{\link{igraph-es-indexing2}},
\code{\link{igraph-vs-indexing}},
\code{\link{igraph-vs-indexing2}},
\code{\link{intersection.igraph.es}()},
\code{\link{intersection.igraph.vs}()},
\code{\link{rev.igraph.es}()},
\code{\link{rev.igraph.vs}()},
\code{\link{union.igraph.es}()},
\code{\link{union.igraph.vs}()},
\code{\link{unique.igraph.es}()},
\code{\link{unique.igraph.vs}()}
}
\concept{vertex and edge sequence operations}
\concept{vertex and edge sequences}
|