expand.grid
Create a Data Frame from All Combinations of Factors

Description

Create a data frame containing all combinations of the values in the input vectors.

Usage

expand.grid(..., KEEP.OUT.ATTRS = TRUE, stringsAsFactors = TRUE)

Arguments

... vector and factor arguments, typically with no repeated values. The arguments can be named, in which case the names are used in the result. Default variable names are "Var" followed by an integer. If a single list is supplied, then its components (and names) are treated as the ... arguments.
KEEP.OUT.ATTRS A logical value. If TRUE, the default, the returned data.frame will have an out.attrs attribute, which contains information about the input vectors of this call to expand.grid. Otherwise, the out.attrs attributed will not be attached to the output.
stringsAsFactors A logical value. If TRUE, the default, convert character arguments to factors whose levels are the unique strings in the argument (in the order in which they were encountered in the character vector).
Value
A data frame containing all combinations of the values in the ... arguments. There will be a column for each input argument and a row for each combination. Values in the first column cycle the fastest, followed by the second, etc.
If KEEP.OUT.ATTRS = TRUE, the result contains an attribute, out.attrs, which is a list of attributes dim and dimnames, which are suitable for turning the result into an array.
See Also
data.frame, gl.
Examples
fit <- lm(uptake ~ Type + Treatment, data=CO2)
grid <- with(CO2, expand.grid(Type=unique(Type), Treatment=unique(Treatment)))
cbind(grid, "Predicted Uptake"=predict(fit, newdata=grid))
Package base version 6.1.1-7
Package Index