This help topic is for R version 1.7.1. For the current version of R, try https://stat.ethz.ch/R-manual/R-patched/library/base/html/data.frame.html
data.frame {base}R Documentation

Data Frames

Description

These functions create or manipulate data frames, tightly coupled collections of variables which share many of the properties of matrices and of lists, used as the fundamental data structure by most of R's modeling software.

Usage

data.frame(..., row.names = NULL, check.rows = FALSE,
        check.names = TRUE)

as.data.frame(x, row.names = NULL, optional = FALSE)
is.data.frame(x)

row.names(x)
row.names(x) <- value

Arguments

...

these arguments are of either the form value or tag=value. Component names are created based on the tag (if present) or the deparsed argument itself.

row.names

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

check.rows

if TRUE then the rows are checked for consistency of length and names.

check.names

logical. If TRUE then the names of the variables in the data frame are checked to ensure that they are syntactically valid variable names. If necessary they are adjusted (by make.names) so that they are.

optional

logical. If TRUE, setting row names is optional.

x

object of class data.frame.

value

a vector with the same length as the number of rows of x, to be coerced to character. Duplicated or missing values are not allowed.

Details

A data frame is a list of variables of the same length with unique row names, given class "data.frame".

Character variables passed to data.frame are converted to factor columns unless protected by I. It also applies to adding columns to a data frame.

If a list or data frame or matrix is passed to data.frame it is as if each column had been passed as a separate argument. This can be overridden if the object past 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"

Objects passed to data.frame should have the same number of rows, but atomic vectors and factors will be recycled a whole number of times if necessary.

Value

For data.frame(.) a data frame, a matrix-like stucture whose columns may be of differing types (numeric, factor and character).

as.data.frame is generic function with many methods. It attempts to coerce its argument to be a data frame.

is.data.frame returns TRUE if its argument is a data frame and FALSE otherwise.

row.names can be used to set and retrieve the row names of a data frame, similarly to rownames for arrays (and it is a generic function that calls rownames for an array argument).

Note

In versions of R prior to 1.4.0 (and in S3 but not S4) logical columns were converted to factors.

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

I, print.data.frame, read.table, Math.data.frame etc, about Group methods for data.frames; make.names.

Examples

L3 <- LETTERS[1:3]
str(d <- data.frame(cbind(x=1, y=1:10), fac=sample(L3, 10, repl=TRUE)))

## The same with automatic column names:
str(     data.frame(cbind(  1,   1:10),     sample(L3, 10, repl=TRUE)))
is.data.frame(d)

## do not convert to factor, using I() :
str(cbind(d, char = I(letters[1:10])), vec.len = 10)

stopifnot(1:10 == row.names(d))# {coercion}

(d0  <- d[, FALSE]) # NULL data frame with 10 rows
(d.0 <- d[FALSE, ]) # <0 rows> data frame  (3 cols)
(d00 <- d0[FALSE,])  # NULL data frame with 0 rows

[Package base version 1.7.1 ]