| data.frame | R Documentation |
The functiondata.frame() creates data frames, tightly coupledcollections of variables which share many of the properties ofmatrices and of lists, used as the fundamental data structure by mostofR's modeling software.
data.frame(..., row.names = NULL, check.rows = FALSE, check.names = TRUE, fix.empty.names = TRUE, stringsAsFactors = FALSE)default.stringsAsFactors() # << this is deprecated !
... | these arguments are of either the form |
row.names |
|
check.rows | if |
check.names | logical. If |
fix.empty.names | logical indicating if arguments which are“unnamed” (in the sense of not being formally called as |
stringsAsFactors | logical: should character vectors be convertedto factors? The ‘factory-fresh’ default has been |
A data frame is a list of variables of the same number of rows withunique row names, given class"data.frame". If no variablesare included, the row names determine the number of rows.
The column names should be non-empty, and attempts to use empty nameswill have unsupported results. Duplicate column names are allowed,but you need to usecheck.names = FALSE fordata.frameto generate such a data frame. However, not all operations on dataframes will preserve duplicated column names: for example matrix-likesubsetting will force column names in the result to be unique.
data.frame converts each of its arguments to a data frame bycallingas.data.frame(optional = TRUE). As that is ageneric function, methods can be written to change the behaviour ofarguments according to their classes:R comes with many such methods.Character variables passed todata.frame are converted tofactor columns unless protected byI or argumentstringsAsFactors is false. If a list or dataframe or matrix is passed todata.frame it is as if eachcomponent or column had been passed as a separate argument (except formatrices protected byI).
Objects passed todata.frame should have the same number ofrows, but atomic vectors (seeis.vector), factors andcharacter vectors protected byI will be recycled awhole number of times if necessary (including as elements of listarguments).
If row names are not supplied in the call todata.frame, therow names are taken from the first component that has suitable names,for example a named vector or a matrix with rownames or a data frame.(If that component is subsequently recycled, the names are discardedwith a warning.) Ifrow.names was supplied asNULL or nosuitable component was found the row names are the integer sequencestarting at one (and such row names are considered to be‘automatic’, and not preserved byas.matrix).
If row names are supplied of length one and the data frame has asingle row, therow.names is taken to specify the row names andnot a column (by name or number).
Names are removed from vector inputs not protected byI.
default.stringsAsFactors is a utility that takesgetOption("stringsAsFactors") and ensures the result isTRUE orFALSE (or throws an error if the value is notNULL). This function isdeprecated now and will no longerbe available in the future.
A data frame, a matrix-like structure whose columns may be ofdiffering types (numeric, logical, factor and character and so on).
How the names of the data frame are created is complex, and the restof this paragraph is only the basic story. If the arguments are allnamed and simple objects (not lists, matrices of data frames) then theargument names give the column names. For an unnamed simple argument,a deparsed version of the argument is used as the name (with anenclosingI(...) removed). For a named matrix/list/data frameargument with more than one named column, the names of the columns arethe name of the argument followed by a dot and the column name insidethe argument: if the argument is unnamed, the argument's column namesare used. For a named or unnamed matrix/list/data frame argument thatcontains a single column, the column name in the result is the columnname in the argument. Finally, the names are adjusted to be uniqueand syntactically valid unlesscheck.names = FALSE.
In versions ofR prior to 2.4.0row.names had to becharacter: to ensure compatibility with such versions ofR, supplya character vector as therow.names argument.
Chambers, J. M. (1992)Data for models.Chapter 3 ofStatistical Models in Seds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
I,plot.data.frame,print.data.frame,row.names,names (for the column names),[.data.frame for subsetting methods andI(matrix(..)) examples;Math.data.frame etc, aboutGroup methods fordata.frames;read.table,make.names,list2DF for creating data frames from lists of variables.
L3 <- LETTERS[1:3]fac <- sample(L3, 10, replace = TRUE)(d <- data.frame(x = 1, y = 1:10, fac = fac))## The "same" with automatic column names:data.frame(1, 1:10, sample(L3, 10, replace = TRUE))is.data.frame(d)## do not convert to factor, using I() :(dd <- cbind(d, char = I(letters[1:10])))rbind(class = sapply(dd, class), mode = sapply(dd, mode))stopifnot(1:10 == row.names(d)) # {coercion}(d0 <- d[, FALSE]) # data frame with 0 columns and 10 rows(d.0 <- d[FALSE, ]) # <0 rows> data frame (3 named cols)(d00 <- d0[FALSE, ]) # data frame with 0 columns and 0 rowsAdd the following code to your website.
For more information on customizing the embed code, readEmbedding Snippets.