| 42333 |
ripley |
1 |
% File src/library/base/man/data.frame.Rd
|
|
|
2 |
% Part of the R package, http://www.R-project.org
|
| 62740 |
maechler |
3 |
% Copyright 1995-2013 R Core Team
|
| 42333 |
ripley |
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
| 2 |
r |
6 |
\name{data.frame}
|
|
|
7 |
\title{Data Frames}
|
| 56186 |
murdoch |
8 |
\alias{data.frame}
|
| 38195 |
ripley |
9 |
\alias{default.stringsAsFactors}
|
| 11763 |
hornik |
10 |
\description{
|
| 24794 |
ripley |
11 |
This function creates data frames, tightly coupled
|
| 11763 |
hornik |
12 |
collections of variables which share many of the properties of
|
|
|
13 |
matrices and of lists, used as the fundamental data structure by most
|
|
|
14 |
of \R's modeling software.
|
|
|
15 |
}
|
|
|
16 |
\usage{
|
| 30912 |
ripley |
17 |
data.frame(\dots, row.names = NULL, check.rows = FALSE,
|
| 38195 |
ripley |
18 |
check.names = TRUE,
|
|
|
19 |
stringsAsFactors = default.stringsAsFactors())
|
|
|
20 |
|
|
|
21 |
default.stringsAsFactors()
|
| 11763 |
hornik |
22 |
}
|
| 2 |
r |
23 |
\arguments{
|
| 48 |
hornik |
24 |
\item{\dots}{these arguments are of either the form \code{value} or
|
| 38139 |
ripley |
25 |
\code{tag = value}. Component names are created based on the tag (if
|
| 48 |
hornik |
26 |
present) or the deparsed argument itself.}
|
| 37787 |
ripley |
27 |
\item{row.names}{\code{NULL} or a single integer or character string
|
|
|
28 |
specifying a column to be used as row names, or a character or
|
|
|
29 |
integer vector giving the row names for the data frame.}
|
| 1040 |
ihaka |
30 |
\item{check.rows}{if \code{TRUE} then the rows are checked for
|
|
|
31 |
consistency of length and names.}
|
| 11763 |
hornik |
32 |
\item{check.names}{logical. If \code{TRUE} then the names of the
|
|
|
33 |
variables in the data frame are checked to ensure that they are
|
| 30004 |
ripley |
34 |
syntactically valid variable names and are not duplicated.
|
|
|
35 |
If necessary they are adjusted (by \code{\link{make.names}})
|
|
|
36 |
so that they are.}
|
| 38195 |
ripley |
37 |
\item{stringsAsFactors}{logical: should character vectors be converted
|
| 43770 |
ripley |
38 |
to factors? The \sQuote{factory-fresh} default is \code{TRUE}, but
|
|
|
39 |
this can be changed by setting \code{\link{options}(stringsAsFactors
|
| 46609 |
hornik |
40 |
= FALSE)}.}
|
| 2 |
r |
41 |
}
|
|
|
42 |
\value{
|
| 27625 |
ripley |
43 |
A data frame, a matrix-like structure whose columns may be of
|
| 24794 |
ripley |
44 |
differing types (numeric, logical, factor and character and so on).
|
| 52676 |
maechler |
45 |
|
| 47211 |
ripley |
46 |
How the names of the data frame are created is complex, and the rest
|
|
|
47 |
of this paragraph is only the basic story. If the arguments are all
|
|
|
48 |
named and simple objects (not lists, matrices of data frames) then the
|
|
|
49 |
argument names give the column names. For an unnamed simple argument,
|
|
|
50 |
a deparsed version of the argument is used as the name (with an
|
|
|
51 |
enclosing \code{I(...)} removed). For a named matrix/list/data frame
|
| 51572 |
murdoch |
52 |
argument with more than one named column, the names of the columns are
|
| 47211 |
ripley |
53 |
the name of the argument followed by a dot and the column name inside
|
|
|
54 |
the argument: if the argument is unnamed, the argument's column names
|
|
|
55 |
are used. For a named or unnamed matrix/list/data frame argument that
|
|
|
56 |
contains a single column, the column name in the result is the column
|
|
|
57 |
name in the argument. Finally, the names are adjusted to be unique
|
|
|
58 |
and syntactically valid unless \code{check.names = FALSE}.
|
| 2 |
r |
59 |
}
|
| 47211 |
ripley |
60 |
|
| 6994 |
pd |
61 |
\details{
|
| 47972 |
ripley |
62 |
A data frame is a list of variables of the same number of rows with
|
|
|
63 |
unique row names, given class \code{"data.frame"}. If no variables
|
|
|
64 |
are included, the row names determine the number of rows.
|
| 52676 |
maechler |
65 |
|
| 49547 |
ripley |
66 |
The column names should be non-empty, and attempts to use empty names
|
|
|
67 |
will have unsupported results. Duplicate column names are allowed,
|
|
|
68 |
but you need to use \code{check.names = FALSE} for \code{data.frame}
|
|
|
69 |
to generate such a data frame. However, not all operations on data
|
|
|
70 |
frames will preserve duplicated column names: for example matrix-like
|
|
|
71 |
subsetting will force column names in the result to be unique.
|
| 21074 |
ripley |
72 |
|
| 24794 |
ripley |
73 |
\code{data.frame} converts each of its arguments to a data frame by
|
| 61150 |
ripley |
74 |
calling \code{\link{as.data.frame}(optional = TRUE)}. As that is a
|
| 24794 |
ripley |
75 |
generic function, methods can be written to change the behaviour of
|
|
|
76 |
arguments according to their classes: \R comes with many such methods.
|
| 15501 |
ripley |
77 |
Character variables passed to \code{data.frame} are converted to
|
| 47972 |
ripley |
78 |
factor columns unless protected by \code{\link{I}} or argument
|
|
|
79 |
\code{stringsAsFactors} is false. If a list or data
|
| 26986 |
ripley |
80 |
frame or matrix is passed to \code{data.frame} it is as if each
|
| 43440 |
ripley |
81 |
component or column had been passed as a separate argument (except for
|
|
|
82 |
matrices of class \code{"\link{model.matrix}"} and those protected by
|
|
|
83 |
\code{\link{I}}).
|
| 20269 |
ripley |
84 |
|
|
|
85 |
Objects passed to \code{data.frame} should have the same number of
|
| 24794 |
ripley |
86 |
rows, but atomic vectors, factors and character vectors protected by
|
| 47045 |
ripley |
87 |
\code{\link{I}} will be recycled a whole number of times if necessary
|
| 57520 |
ripley |
88 |
(including as elements of list arguments).
|
| 24794 |
ripley |
89 |
|
|
|
90 |
If row names are not supplied in the call to \code{data.frame}, the
|
|
|
91 |
row names are taken from the first component that has suitable names,
|
|
|
92 |
for example a named vector or a matrix with rownames or a data frame.
|
| 27625 |
ripley |
93 |
(If that component is subsequently recycled, the names are discarded
|
| 24794 |
ripley |
94 |
with a warning.) If \code{row.names} was supplied as \code{NULL} or no
|
|
|
95 |
suitable component was found the row names are the integer sequence
|
| 40307 |
ripley |
96 |
starting at one (and such row names are considered to be
|
|
|
97 |
\sQuote{automatic}, and not preserved by \code{\link{as.matrix}}).
|
| 24794 |
ripley |
98 |
|
|
|
99 |
If row names are supplied of length one and the data frame has a
|
|
|
100 |
single row, the \code{row.names} is taken to specify the row names and
|
|
|
101 |
not a column (by name or number).
|
| 29352 |
ripley |
102 |
|
|
|
103 |
Names are removed from vector inputs not protected by \code{\link{I}}.
|
| 38195 |
ripley |
104 |
|
|
|
105 |
\code{default.stringsAsFactors} is a utility that takes
|
|
|
106 |
\code{\link{getOption}("stringsAsFactors")} and ensures the result is
|
| 43770 |
ripley |
107 |
\code{TRUE} or \code{FALSE} (or throws an error if the value is not
|
|
|
108 |
\code{NULL}).
|
| 6994 |
pd |
109 |
}
|
| 15501 |
ripley |
110 |
\note{
|
| 37787 |
ripley |
111 |
In versions of \R prior to 2.4.0 \code{row.names} had to be
|
| 47972 |
ripley |
112 |
character: to ensure compatibility with such versions of \R, supply
|
| 37787 |
ripley |
113 |
a character vector as the \code{row.names} argument.
|
| 15501 |
ripley |
114 |
}
|
| 24239 |
ripley |
115 |
\references{
|
|
|
116 |
Chambers, J. M. (1992)
|
|
|
117 |
\emph{Data for models.}
|
|
|
118 |
Chapter 3 of \emph{Statistical Models in S}
|
| 47262 |
ripley |
119 |
eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
|
| 24239 |
ripley |
120 |
}
|
| 2 |
r |
121 |
\seealso{
|
| 20171 |
ripley |
122 |
\code{\link{I}},
|
| 30461 |
ripley |
123 |
\code{\link{plot.data.frame}},
|
| 15518 |
ripley |
124 |
\code{\link{print.data.frame}},
|
| 39531 |
ripley |
125 |
\code{\link{row.names}}, \code{\link{names}} (for the column names),
|
| 52676 |
maechler |
126 |
\code{\link{[.data.frame}} for subsetting methods,% ./Extract.data.frame.Rd
|
| 11763 |
hornik |
127 |
\code{\link{Math.data.frame}} etc, about
|
|
|
128 |
\emph{Group} methods for \code{data.frame}s;
|
| 24798 |
ripley |
129 |
\code{\link{read.table}},
|
| 11763 |
hornik |
130 |
\code{\link{make.names}}.
|
| 2 |
r |
131 |
}
|
| 1176 |
maechler |
132 |
\examples{
|
| 2614 |
maechler |
133 |
L3 <- LETTERS[1:3]
|
| 62740 |
maechler |
134 |
fac <- sample(L3, 10, replace = TRUE)
|
|
|
135 |
(d <- data.frame(x = 1, y = 1:10, fac = fac))
|
|
|
136 |
\dontshow{% formerly, example was data.frame(cbind(.,.), fac):
|
|
|
137 |
stopifnot(identical(within(d, y <- as.numeric(y)),
|
|
|
138 |
data.frame(cbind(x = 1, y = 1:10), fac)))
|
|
|
139 |
}
|
|
|
140 |
## The "same" with automatic column names:
|
|
|
141 |
data.frame(1, 1:10, sample(L3, 10, replace = TRUE))
|
| 10674 |
maechler |
142 |
|
| 1176 |
maechler |
143 |
is.data.frame(d)
|
| 10674 |
maechler |
144 |
|
| 10675 |
maechler |
145 |
## do not convert to factor, using I() :
|
| 27712 |
ripley |
146 |
(dd <- cbind(d, char = I(letters[1:10])))
|
| 61150 |
ripley |
147 |
rbind(class = sapply(dd, class), mode = sapply(dd, mode))
|
| 10674 |
maechler |
148 |
|
| 61168 |
ripley |
149 |
stopifnot(1:10 == row.names(d)) # {coercion}
|
| 16143 |
maechler |
150 |
|
| 62740 |
maechler |
151 |
(d0 <- d[, FALSE]) # data frame with 0 columns and 10 rows
|
|
|
152 |
(d.0 <- d[FALSE, ]) # <0 rows> data frame (3 named cols)
|
|
|
153 |
(d00 <- d0[FALSE, ]) # data frame with 0 columns and 0 rows
|
| 1176 |
maechler |
154 |
}
|
| 286 |
maechler |
155 |
\keyword{classes}
|
|
|
156 |
\keyword{methods}
|