xy_grid: Create a Cartesian product from evenly spaced values of two...

View source: R/xy_grid.R

xy_gridR Documentation

Create a Cartesian product from evenly spaced values of two variables

Description

Create a Cartesian product from evenly spaced values of two variables.

Usage

xy_grid(x, ...)

## Default S3 method:
xy_grid(x, y, grid.resolution = 51, col.names = NULL, ...)

## S3 method for class 'formula'
xy_grid(x, data, grid.resolution = 51, ...)

## S3 method for class 'matrix'
xy_grid(x, grid.resolution = 51, ...)

## S3 method for class 'data.frame'
xy_grid(x, grid.resolution = 51, ...)

Arguments

x

Either a numeric vector (if argument y is also specified), a matrix-like object (e.g., a data frame), or two-variable formula of the form y ~ x.

...

Additional (optional) arguments. (Currently ignored.)

y

A numeric vector representing the second variable (only required if x is a numeric vector).

grid.resolution

Integer specifying the number of equally-spaced values to use for each numeric variable. For example, if grid.resolution = k, then the final data frame will have k^2 rows (formed by a Cartesian product).

col.names

Optional vector of column names to use for the output whenever both x and y are supplied.

data

A data frame containing the variables specified in x if x is a formula.

formula

A two-variable formula of the form y ~ x. The response (i.e., the variable on the left side of the formula) corresponds to the second column of the output.

Value

A data frame representing the Cartesian product between equally spaced values from each variable.

Examples

x1 <- 1:3
x2 <- letters[1L:3L]
xy_grid(x1, x2, gr = 3, col.names = c("x1", "x2"))  # will have 3^2=9 rows
xy_grid(m <- cbind(x1, x2), gr = 3)     # equivalent
xy_grid(d <- as.data.frame(m), gr = 3)  # equivalent
xy_grid(x2 ~ x1, data = d, gr = 3)      # equivalent

bgreenwell/treemisc documentation built on Oct. 26, 2022, 12:56 a.m.