as.data.frame {base} | R Documentation |
Coerce to a Data Frame
Description
Functions to check if an object is a data frame, or coerce it if possible.
Usage
as.data.frame(x, row.names = NULL, optional = FALSE, ...)
## S3 method for class 'character'
as.data.frame(x, ...,
stringsAsFactors = FALSE)
## S3 method for class 'list'
as.data.frame(x, row.names = NULL, optional = FALSE, ...,
cut.names = FALSE, col.names = names(x), fix.empty.names = TRUE,
new.names = !missing(col.names),
check.names = !optional,
stringsAsFactors = FALSE)
## S3 method for class 'matrix'
as.data.frame(x, row.names = NULL, optional = FALSE,
make.names = TRUE, ...,
stringsAsFactors = FALSE)
as.data.frame.vector(x, row.names = NULL, optional = FALSE, ...,
nm = deparse1(substitute(x)))
is.data.frame(x)
Arguments
x |
any R object. |
row.names |
|
optional |
logical. If |
... |
additional arguments to be passed to or from methods. |
stringsAsFactors |
logical: should the character vector be converted to a factor? |
cut.names |
logical or integer; indicating if column names with
more than 256 (or |
col.names |
(optional) character vector of column names. |
fix.empty.names |
logical indicating if empty column names, i.e.,
|
new.names |
logical indicating that |
check.names |
logical; passed to the |
make.names |
a |
nm |
a |
Details
as.data.frame
is a generic function with many methods, and
users and packages can supply further methods. For classes that act
as vectors, often a copy of as.data.frame.vector
will work
as the method.
Since R 4.3.0, the default method will call
as.data.frame.vector
for atomic (as by is.atomic
) x
.
Direct calls of as.data.frame.class
are still possible (base package!),
for 12 atomic base classes, but are deprecated
where calling as.data.frame.vector
instead is recommended.
If a list is supplied, each element is converted to a column in the
data frame. Similarly, each column of a matrix is converted separately.
This can be overridden if the object has a class which has
a method for as.data.frame
: two examples are
matrices of class "model.matrix"
(which are
included as a single column) and list objects of class
"POSIXlt"
which are coerced to class
"POSIXct"
.
Arrays can be converted to data frames. One-dimensional arrays are treated like vectors and two-dimensional arrays like matrices. Arrays with more than two dimensions are converted to matrices by ‘flattening’ all dimensions after the first and creating suitable column labels.
Character variables are converted to factor columns unless protected
by I
.
If a data frame is supplied, all classes preceding "data.frame"
are stripped, and the row names are changed if that argument is supplied.
If row.names = NULL
, row names are constructed from the names
or dimnames of x
, otherwise are the integer sequence
starting at one. Few of the methods check for duplicated row names.
Names are removed from vector columns unless I
.
Value
as.data.frame
returns a data frame, normally with all row names
""
if optional = TRUE
.
is.data.frame
returns TRUE
if its argument is a data
frame (that is, has "data.frame"
amongst its classes)
and FALSE
otherwise.
References
Chambers, J. M. (1992) Data for models. Chapter 3 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
See Also
data.frame
and make.names
;
as.data.frame.table
for the table
method (which has
additional arguments).
Examples
L0 <- list(LETTERS[1:7], c(4L, 2:3, 5:7, 1L))
L <- L0; names(L) <- nms <- c("nam", "age")
d0 <- as.data.frame(L0, col.names = nms)
(d1 <- as.data.frame(L))
stopifnot(identical(d0, d1))
## showing possibilities on how NA names are handled:
L <- list(A = 1:4); names(L) <- NA
names(dL <- as.data.frame(L)) # "NA."
names(dL1 <- as.data.frame(L, col.names = names(L))) # "NA."
##
names(dL1.<- as.data.frame(L, check.names=FALSE)) # "NA"
##
names(dL2 <- as.data.frame(L, col.names = names(L), check.names=FALSE)) # NA
names(dLn <- as.data.frame(L, new.names = TRUE, check.names=FALSE)) # NA