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, ...)
as.data.frame.model.matrix(x, row.names = NULL, optional = FALSE, ...) 

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, keep the object default column names with no change.
  • If FALSE (the default), 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).

Details

In as.data.frame.model.matrix method, if optional = FALSE, the row name of x will be 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. (1992) Data for models. Chapter 3 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, 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 4.0.0-28
Package Index