\Header{Extract}{Extract or Replace Parts of an Object}
\alias{\$}{Extract}
\alias{\$<\Rdash}{Extract}
\alias{.subset}{Extract}
\alias{.subset2}{Extract}
\alias{Subscript}{Extract}
\alias{[}{Extract}
\methalias{[.formula}{Extract}
\methalias{[.ts}{Extract}
\alias{[<\Rdash}{Extract}
\alias{[[}{Extract}
\alias{[[<\Rdash}{Extract}
\keyword{array}{Extract}
\keyword{list}{Extract}
\begin{Description}\relax
Operators act on vectors, arrays and lists to extract or
replace subsets.\end{Description}
\begin{Usage}
\begin{verbatim}
x[i]
x[i, j, ... , drop=TRUE]
x[[i]]
x[[i, j, ...]]
x$name

.subset(x, ...)
.subset2(x, ...)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{x}] object from which to extract elements or in which to replace
elements.
\item[\code{i, j, ..., name}] elements to extract or replace. \code{i, j} are
\code{numeric} or \code{character} or empty whereas \code{name} must be
character or an (unquoted) name.  Numeric values are coerced to
integer as by \code{\Link{as.integer}}.

For \code{[}-indexing only: \code{i, j, ...} can be
logical vectors, indicating elements/slices to select. Such vectors
are recycled if necessary to match the corresponding extent. When
indexing arrays, \code{i} can be a (single) matrix with as many
columns as there are dimensions of \code{x}; the result is then a
vector with elements corresponding to the sets of indices in each
row of \code{i}.
\item[\code{drop}] For matrices, and arrays.  If \code{TRUE} the
result is coerced to the lowest possible dimension (see examples
below). This only works for extracting elements, not for the
replacement forms.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
These operators are generic.  You can write methods to handle subsetting
of specific classes of objects, see \link{InternalMethods} as well as
\code{\Link{[.data.frame}} and \code{\Link{[.factor}}.  The
descriptions here apply only to the default methods.

The most important distinction between \code{[}, \code{[[} and
\code{\$} is that the \code{[} can select more than one element whereas
the other two select a single element.  \code{\$} does not allow
computed indices, whereas \code{[[} does.  \code{x\$name} is equivalent
to \code{x[["name"]]} if \code{x} is recursive
(see \code{\Link{is.recursive}}) and \code{NULL} otherwise.

The \code{[[} operator requires all relevant subscripts to be supplied.
With the \code{[} operator an empty index (a comma separated blank)
indicates that all entries in that dimension are selected.

If one of these expressions appears on the left side of an assignment
then that part of \code{x} is set to the value of the right hand side
of the assignment.

Indexing by factors is allowed and is equivalent to indexing by the
numeric codes (see \code{\Link{factor}}) and not by the character
values which are printed (for which use \code{[as.character(i)]}).

When operating on a list, the \code{[[} operator gives the specified
element of the list while the \code{[} operator returns a list with
the specified element(s) in it.

As from \R{} 1.7.0 \code{[[} can be applied recursively to lists, so
that if the single index \code{i} is a vector of length \code{p},
\code{alist[[i]]} is equivalent to \code{alist[[i1]]...[[ip]]}
providing all but the final indexing results in a list.

The operators \code{\$} and \code{\$<-} do not evaluate their second
argument.  It is translated to a string and that string is used to
locate the correct component of the first argument.

When \code{\$<-} is applied to a \code{NULL} \code{x}, it coerces
\code{x} to \code{list()}.  This is what happens with \code{[[<-} is
\code{y} is of length greater than one: if \code{y} has length 1 or 0,
\code{x} is coerced to a zero-length vector of the type of \code{value}, 

The functions \code{.subset} and \code{.subset2} are essentially
equivalent to the \code{[} and \code{[[} operators, except that
methods dispatch does not take place.  This is to avoid expensive
unclassing when applying the default method to an object.  They
should not normally be invoked by end users.\end{Details}
\begin{References}\relax
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
\emph{The New S Language}.
Wadsworth \& Brooks/Cole.\end{References}
\begin{SeeAlso}\relax
\code{\Link{list}}, \code{\Link{array}}, \code{\Link{matrix}}.

\code{\Link{[.data.frame}} and \code{\Link{[.factor}} for the
behaviour when applied to data.frame and factors.

\code{\Link{Syntax}} for operator precedence, and the
\emph{R Language} reference manual about indexing details.\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
x <- 1:12; m <- matrix(1:6,nr=2); li <- list(pi=pi, e = exp(1))
x[10]                 # the tenth element of x
m[1,]                 # the first row of matrix m
m[1, , drop = FALSE]  # is a 1-row matrix
m[,c(TRUE,FALSE,TRUE)]# logical indexing
m[cbind(c(1,2,1),3:1)]# matrix index
li[[1]]               # the first element of list li
y <- list(1,2,a=4,5)
y[c(3,4)]             # a list containing elements 3 and 4 of y
y$a                   # the element of y named a

## non-integer indices are truncated:
(i <- 3.999999999) # "4" is printed
(1:5)[i]  # 3

## recursive indexing into lists
z <- list( a=list( b=9, c='hello'), d=1:5)
unlist(z)
z[[c(1, 2)]]
z[[c(1, 2, 1)]]  # both "hello"
z[[c("a", "b")]] <- "new"
unlist(z)
\end{ExampleCode}
\end{Examples}

