\HeaderA{data.frame}{Data Frames}{data.frame}
\aliasA{default.stringsAsFactors}{data.frame}{default.stringsAsFactors}
\keyword{classes}{data.frame}
\keyword{methods}{data.frame}
\begin{Description}\relax
This function creates data frames, tightly coupled
collections of variables which share many of the properties of
matrices and of lists, used as the fundamental data structure by most
of \R's modeling software.
\end{Description}
\begin{Usage}
\begin{verbatim}
data.frame(..., row.names = NULL, check.rows = FALSE,
           check.names = TRUE,
           stringsAsFactors = default.stringsAsFactors())

default.stringsAsFactors()
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{...}] these arguments are of either the form \code{value} or
\code{tag = value}.  Component names are created based on the tag (if
present) or the deparsed argument itself.
\item[\code{row.names}] \code{NULL} or a single integer or character string
specifying a column to be used as row names, or a character or
integer vector giving the row names for the data frame.
\item[\code{check.rows}] if \code{TRUE} then the rows are checked for
consistency of length and names.
\item[\code{check.names}] logical.  If \code{TRUE} then the names of the
variables in the data frame are checked to ensure that they are
syntactically valid variable names and are not duplicated.
If necessary they are adjusted (by \code{\LinkA{make.names}{make.names}})
so that they are.
\item[\code{stringsAsFactors}] logical: should character vectors be converted
to factors?  The \sQuote{factory-fresh} default is \code{TRUE}, but
this can be changed by setting \code{\LinkA{options}{options}(stringsAsFactors
      = FALSE}.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
A data frame is a list of variables of the same length with unique row
names, given class \code{"data.frame"}.  If there are zero variables,
the row names determine the number of rows.


Duplicate column names are allowed, but you need to use
\code{check.names=FALSE} for \code{data.frame} to generate such a data
frame.  However, not all operations on data frames will preserve
duplicated column names: for example matrix-like subsetting will force
column names in the result to be unique.

\code{data.frame} converts each of its arguments to a data frame by
calling \code{\LinkA{as.data.frame}{as.data.frame}(optional=TRUE)}.  As that is a
generic function, methods can be written to change the behaviour of
arguments according to their classes: \R{} comes with many such methods.
Character variables passed to \code{data.frame} are converted to
factor columns unless protected by \code{\LinkA{I}{I}}.  If a list or data
frame or matrix is passed to \code{data.frame} it is as if each
component or column had been passed as a separate argument (except for
matrices of class \code{"\LinkA{model.matrix}{model.matrix}"} and those protected by
\code{\LinkA{I}{I}}).

Objects passed to \code{data.frame} should have the same number of
rows, but atomic vectors, factors and character vectors protected by
\code{\LinkA{I}{I}} will be recycled a whole number of times if necessary.

If row names are not supplied in the call to \code{data.frame}, the
row names are taken from the first component that has suitable names,
for example a named vector or a matrix with rownames or a data frame.
(If that component is subsequently recycled, the names are discarded
with a warning.)  If \code{row.names} was supplied as \code{NULL} or no
suitable component was found the row names are the integer sequence
starting at one (and such row names are considered to be
\sQuote{automatic}, and not preserved by \code{\LinkA{as.matrix}{as.matrix}}).

If row names are supplied of length one and the data frame has a
single row, the \code{row.names} is taken to specify the row names and
not a column (by name or number).

Names are removed from vector inputs not protected by \code{\LinkA{I}{I}}.

\code{default.stringsAsFactors} is a utility that takes
\code{\LinkA{getOption}{getOption}("stringsAsFactors")} and ensures the result is
\code{TRUE} or \code{FALSE} (or throws an error if the value is not
\code{NULL}).
\end{Details}
\begin{Value}
A data frame, a matrix-like structure whose columns may be of
differing types (numeric, logical, factor and character and so on).
\end{Value}
\begin{Note}\relax
In versions of \R{} prior to 2.4.0 \code{row.names} had to be
character: to ensure compatibility with earlier versions of \R, supply
a character vector as the \code{row.names} argument.
\end{Note}
\begin{References}\relax
Chambers, J. M. (1992)
\emph{Data for models.}
Chapter 3 of \emph{Statistical Models in S}
eds J. M. Chambers and T. J. Hastie, Wadsworth \& Brooks/Cole.
\end{References}
\begin{SeeAlso}\relax
\code{\LinkA{I}{I}},
\code{\LinkA{plot.data.frame}{plot.data.frame}},
\code{\LinkA{print.data.frame}{print.data.frame}},
\code{\LinkA{row.names}{row.names}}, \code{\LinkA{names}{names}} (for the column names),
\code{\LinkA{[.data.frame}{[.data.frame}} for subsetting methods,
\code{\LinkA{Math.data.frame}{Math.data.frame}} etc, about
\emph{Group} methods for \code{data.frame}s;
\code{\LinkA{read.table}{read.table}},
\code{\LinkA{make.names}{make.names}}.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
L3 <- LETTERS[1:3]
(d <- data.frame(cbind(x=1, y=1:10), fac=sample(L3, 10, replace=TRUE)))

## The same with automatic column names:
data.frame(cbind(  1,   1:10),     sample(L3, 10, replace=TRUE))

is.data.frame(d)

## do not convert to factor, using I() :
(dd <- cbind(d, char = I(letters[1:10])))
rbind(class=sapply(dd, class), mode=sapply(dd, mode))

stopifnot(1:10 == row.names(d))# {coercion}

(d0  <- d[, FALSE]) # NULL data frame with 10 rows
(d.0 <- d[FALSE, ]) # <0 rows> data frame  (3 cols)
(d00 <- d0[FALSE,])  # NULL data frame with 0 rows
\end{ExampleCode}
\end{Examples}

