A data frame is a list of variables of the same number of rows with
unique row names, given class "data.frame"
. If no variables
are included, the row names determine the number of rows.
The column names should be non-empty, and attempts to use empty names
will have unsupported results. Duplicate column names are allowed,
but you need to use check.names = FALSE
for 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.
data.frame
converts each of its arguments to a data frame by
calling 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 data.frame
are converted to
factor columns unless protected by I
or argument
stringsAsFactors
is false. If a list or data
frame or matrix is passed to data.frame
it is as if each
component or column had been passed as a separate argument (except for
matrices protected by I
).
Objects passed to data.frame
should have the same number of
rows, but atomic vectors (see is.vector
), factors and
character vectors protected by I
will be recycled a
whole number of times if necessary (including as elements of list
arguments).
If row names are not supplied in the call to 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 row.names
was supplied as 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
‘automatic’, and not preserved by as.matrix
).
If row names are supplied of length one and the data frame has a
single row, the 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 I
.
default.stringsAsFactors
is a utility that takes
getOption("stringsAsFactors")
and ensures the result is
TRUE
or FALSE
(or throws an error if the value is not
NULL
).