\HeaderA{subset}{Subsetting Vectors, Matrices and Data Frames}{subset}
\methaliasA{subset.data.frame}{subset}{subset.data.frame}
\methaliasA{subset.default}{subset}{subset.default}
\methaliasA{subset.matrix}{subset}{subset.matrix}
\keyword{manip}{subset}
\begin{Description}\relax
Return subsets of vectors, matrices or data frames which meet conditions.
\end{Description}
\begin{Usage}
\begin{verbatim}
subset(x, ...)

## Default S3 method:
subset(x, subset, ...)

## S3 method for class 'matrix':
subset(x, subset, select, drop = FALSE, ...)

## S3 method for class 'data.frame':
subset(x, subset, select, drop = FALSE, ...)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{x}] object to be subsetted.
\item[\code{subset}] logical expression indicating elements or rows to keep:
missing values are taken as false.
\item[\code{select}] expression, indicating columns to select from a
data frame.
\item[\code{drop}] passed on to \code{[} indexing operator.
\item[\code{...}] further arguments to be passed to or from other methods.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
This is a generic function, with methods supplied for matrices, data
frames and vectors (including lists).  Packages and users can add
further methods.

For ordinary vectors, the result is simply
\code{x[subset \& !is.na(subset)]}.

For data frames, the \code{subset} argument works on the rows.  Note
that \code{subset} will be evaluated in the data frame, so columns can
be referred to (by name) as variables in the expression (see the examples).

The \code{select} argument exists only for the methods for data frames
and matrices.  It works by first replacing column names in the
selection expression with the corresponding column numbers in the data
frame and then using the resulting integer vector to index the
columns.  This allows the use of the standard indexing conventions so
that for example ranges of columns can be specified easily, or single
columns can be dropped (see the examples).

The \code{drop} argument is passed on to the indexing method for
matrices and data frames: note that the default for matrices is
different from that for indexing.
\end{Details}
\begin{Value}
An object similar to \code{x} contain just the selected elements (for
a vector), rows and columns (for a matrix or data frame), and so on.
\end{Value}
\begin{Author}\relax
Peter Dalgaard and Brian Ripley
\end{Author}
\begin{SeeAlso}\relax
\code{\LinkA{[}{[}}, 
\code{\LinkA{transform}{transform}}
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
subset(airquality, Temp > 80, select = c(Ozone, Temp))
subset(airquality, Day == 1, select = -Temp)
subset(airquality, select = Ozone:Wind)

with(airquality, subset(Ozone, Temp > 80))

## sometimes requiring a logical 'subset' argument is a nuisance
nm <- rownames(state.x77)
start_with_M <- nm %in% grep("^M", nm, value=TRUE)
subset(state.x77, start_with_M, Illiteracy:Murder)
\end{ExampleCode}
\end{Examples}

