The R Project SVN R

Rev

Rev 81794 | Details | Compare with Previous | Last modification | View Log | RSS feed

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