as.data.frame
Construct a Data Frame Object from an S object

### Description

All as.data.frame() functions are S3 method implementations of the as.data.frame generic for the various object classes. Each method creates a data frame according to the arguments.

### Usage

    as.data.frame(x, row.names = NULL, optional = FALSE, ..., stringsAsFactors = default.stringsAsFactors())
    as.data.frame.model.matrix(x, row.names = NULL, optional = FALSE, ...)
    as.data.frame.list(x, row.names = NULL, optional = FALSE, ..., cut.names = FALSE, col.names = names(x), fix.empty.names = TRUE, stringsAsFactors = default.stringsAsFactors())

### Arguments

x | an S object.
row.names | the row names of the data frame.
optional | a logical value. (This argument is not available in the default and character class methods). If TRUE, then keep the object default column names with no change. If FALSE (the default), then try to construct the column names with a specified nm argument(if available) or the names of x (if available). Otherwise, construct the column names as a "X*", "V*", or "c*" format. (* can be 1, 2, 3, ..., or combination of character elements).
stringsAsFactors | A logical value. If TRUE, then character strings are represented as factors in the data frame. (This argument applies to character, list, matrix, and table class methods only.) Note that as of version 5.2 of Spotfire Enterprise Runtime for R, default.stringsAsFactors() returns FALSE instead of the previous TRUE unless the user has execututed options(stringsAsFactors=TRUE) earlier in the session. This matches the same change in version 4.0.0 of open-source R.
cut.names | If a numeric value, then column names supplied by the user are cut to less than this number of characters, and " ..." is appended to indicate that the name has been trimmed. cut.names=TRUE means the same as cut.names=256. Names generated from the contents of the column are not affected by this argument. This argument applies only to the list method of as.data.frame.
col.names | A character vector with as many elements as the list x. These elements are used as the column names of the data.frame. These names can be trimmed by the cut.names argument. This argument applies only to the "list" method of as.data.frame.
fix.empty.names | A logical value. If TRUE (the default), then generate names for elements of x that do not have names. The name of a column is based on deparsing the column. This argument applies only to the "list" method of as.data.frame.

### Details

In the as.data.frame.model.matrix method, if optional = FALSE, the row name of x is deparsed.
Value
returns a data frame, depending on the arguments. If x is already a data frame, the value is a data frame like x, except that any classes preceding "data.frame" are dropped, so that "data.frame" becomes the first class.
References
Chambers, J. M. and Hastie, T .J. (Eds.) 1992. Chapter 3: Data for Models. Statistical Models in S. Pacific Grove, CA.: Wadsworth & Brooks/Cole.
See Also
Date, POSIXt, difftime
Examples

    # For class raw, factor, ordered, integer, numeric, complex, logical
    # and vector:
    x <-  c(1:3)
    as.data.frame(x, row.names = c("one", "two", "three"),
       optional = FALSE, nm = c("Data"))
    ##           Data
    ##  one      1
    ##  two      2
    ##  three    3
    as.data.frame(x, row.names = c("one", "two", "three"),
       optional = TRUE, nm = c("Data"))
    ##           structure(c("1", "2", "3"), class = "AsIs")
    ##   one                                             1
    ##   two                                             2
    ##   three                                           3
    # For class character
    x <- c("Red", "Green", "Blue")
    as.data.frame(x, row.names = c("R1", "R2", "R3"))
    ##         x
    ##  R1   Red
    ##  R2 Green
    ##  R3  Blue
    y <- as.data.frame(x, row.names = c("R1", "R2", "R3"),
       stringsAsFactors = FALSE)
    class(y$x)     #   Strings is not converted as factors
    ##  [1] "character"
    y <- as.data.frame(x, row.names = c("R1", "R2", "R3"),
       stringsAsFactors = TRUE)
    class(y$x)    #    Strings is converted as factors
    ##   [1] "factor"
    # For class matrix
    y <- matrix(1:12, nrow = 3)
    as.data.frame(y, row.names = c("One", "Two", "Three"))
    ##        V1 V2 V3 V4
    ##  One    1  4  7 10
    ##  Two    2  5  8 11
    ##  Three  3  6  9 12
    # For class list
    aa <- 1:3
    bb <- c("R", "G", "B")
    cc <- list(aa, bb)
    as.data.frame(cc)
    ##     X1.3          c..R....G....B..
    ## 1    1                R
    ## 2    2                G
    ## 3    3                B
    as.data.frame(cc, optional = TRUE)
    ##    1:3        c("R", "G", "B")
    ## 1   1                R
    ## 2   2                G
    ## 3   3                B
    # For class table
    y <- matrix(1:12, nrow = 3)
    z <- as.table(y)
    as.data.frame(z)
    ##     Var1 Var2 Freq
    ## 1     A    A    1
    ## 2     B    A    2
    ## 3     C    A    3
    ## 4     A    B    4
    ## 5     B    B    5
    ## 6     C    B    6
    ## 7     A    C    7
    ## 8     B    C    8
    ## 9     C    C    9
    ## 10    A    D   10
    ## 11    B    D   11
    ## 12    C    D   12
    # For class ts
    as.data.frame(ts(3:5), row.names = c("I", "II", "III"))
    ##     x
    ## I   3
    ## II  4
    ## III 5
    # For class POSIXlt
    plt <- as.POSIXlt(c("1970-01-01 00:00:00", "2038-01-19 03:14:07"))
    as.data.frame(plt, row.names = c("Unix Epoch", "Unix Millennium Bug"))
    ##                                     plt
    ## Unix Epoch          1970-01-01 00:00:00
    ## Unix Millennium Bug 2038-01-19 03:14:07
    # For class difftime
    dt <- as.difftime(c("01:00:00", "00:01:00", "00:00:01"))
    as.data.frame(dt)
    ##          dt
    ## 1 3600 secs
    ## 2   60 secs
    ## 3    1 secs
    # For class data.frame
    x <- data.frame(matrix(1 : 12, 3))
    # add some classes preceding "data.frame"
    class(x) <- c("foo1", "foo2", "data.frame")
    class(as.data.frame(x))
    ## [1] "data.frame"
    # default
    x  <- 3
    class(x) <- "other"
    ## Not run:
    as.data.frame(x)
    ## Error in as.data.frame.default(x) :
    ##   cannot coerce class "other" into a data.frame

    ## End(Not run)
    # For class model.matrix
    fw <- lm(Fuel ~ Weight, data = Sdatasets::fuel.frame)
    mm.y <- model.matrix(fw)
    as.data.frame.model.matrix(mm.y)
    ##                               mm.y.(Intercept) mm.y.Weight
    ## Eagle Summit 4                               1        2560
    ## Ford Escort 4                                1        2345
    ## Ford Festiva 4                               1        1845
    ##     .
    ##     .
    ##     .
    ## Nissan Van 4                                 1        3690

Package base version 6.1.5-18
Package Index