outer
Generalized Outer Products

Description

Performs an outer product operation given two arrays (or vectors) and, optionally, a function.

Usage

outer(X, Y, FUN = "*", ...)
X %o% Y #operator form

Arguments

X, Y the first and second arguments to the function FUN. Missing values (NAs) are allowed if FUN accepts them.
FUN a function that takes at least two vectors as arguments and returns a single value. This may also be a character string giving the name of such a function.
... other arguments to FUN, if needed. The names of the arguments, if any, must be meaningful to FUN.

Details

First, outer forms two arrays corresponding to the data in X and Y, each of which has a dim attribute formed by concatenating the dim attributes of X and Y.
Second, outer calls FUN just once with these two arrays as arguments. Therefore, FUN should be a function that operates element-wise on vectors or arrays and expects (at least) two arguments.
The outer() function works best with input vectors that can be contained in a matrix or an array. For example, you can use input vectors with the class numeric or character, but input vectors of a more complex class like timeDate or factor might not produce useful results. In general, if you can use the replicate function, rep(), to replicate the data values in the input vectors to any arbitrary length, you can use the input vectors in an outer() calculation.
Value
returns an array whose dimension vector is the concatenation of the dimension vectors of X and Y, such that FUN(X[i,j,k,...], Y[a,b,c,...]) is the value of the [i,j,k,...,a,b,c,...] element. (Vectors are considered to be one-dimensional arrays.)
See Also
crossprod, Matrix-product.
Examples
x <- runif(3)
y <- runif(4)
z <- x %o% y   # The outer product array

# simulate a two-way table outer(c(3.1, 4.5, 2.8, 5.2), c(2.7, 3.1, 2.8), "+") + matrix(rnorm(12), nrow=4) outer(month.name, 2010:2011, paste) # All month year combinations

Package base version 6.1.1-7
Package Index