[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  as.data.frame: 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 character
    as.data.frame(x, …,
                  stringsAsFactors = default.stringsAsFactors())
    
    # S3 method for list
    as.data.frame(x, row.names = NULL, optional = FALSE, …,
                  cut.names = FALSE, col.names = names(x), fix.empty.names = TRUE,
                  stringsAsFactors = default.stringsAsFactors())
    
    # S3 method for matrix
    as.data.frame(x, row.names = NULL, optional = FALSE,
                  make.names = TRUE, …,
                  stringsAsFactors = default.stringsAsFactors())
    
    is.data.frame(x)

##  Arguments

x

any  R  object.

row.names

` NULL ` or a character vector giving the row names for the data frame.
Missing values are not allowed.

optional

logical. If ` TRUE ` , setting row names and converting column names (to
syntactic names: see ` [ make.names
](/link/make.names?package=base&version=3.6.2) ` ) is optional. Note that all
of  R  's  base  package ` as.data.frame() ` methods use ` optional ` only for
column names treatment, basically with the meaning of ` [ data.frame
](/link/data.frame?package=base&version=3.6.2) (*, check.names = !optional) `
. See also the ` make.names ` argument of the ` matrix ` method.

…

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 `
cut.names ` if that is numeric) characters should be shortened (and the last 6
characters replaced by ` " ..." ` ).

col.names

(optional) character vector of column names.

fix.empty.names

logical indicating if empty column names, i.e., ` "" ` should be fixed up (in
` [ data.frame ](/link/data.frame?package=base&version=3.6.2) ` ) or not.

make.names

a ` [ logical ](/link/logical?package=base&version=3.6.2) ` , i.e., one of `
FALSE, NA, TRUE ` , indicating what should happen if the row names (of the
matrix ` x ` ) are invalid. If they are invalid, the default, ` TRUE ` , calls
` [ make.names ](/link/make.names?package=base&version=3.6.2) (*, unique=TRUE)
` ; ` make.names=NA ` will use “automatic” row names and a ` FALSE ` value
will signal an error for invalid row names.

##  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.

##  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.

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
](/link/model.matrix?package=base&version=3.6.2) " ` (which are included as a
single column) and list objects of class ` " [ POSIXlt
](/link/POSIXlt?package=base&version=3.6.2) " ` which are coerced to class ` "
[ POSIXct ](/link/POSIXct?package=base&version=3.6.2) " ` .

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 v