You are currently viewing outer() Function in R with Examples

The outer() in R is used to apply the function to two arrays or vectors to get the result in a Matrix. In this article, I will explain the syntax of the outer() function and how to use this to get the product of two arrays or vectors by applying custom functions.

R outer() Function Key Points

  • The outer in R function is used to create a matrix, tables e.t.c
  • It is used either with a vector or arrays of numeric values.
  • It can also be used with other data types. Need to be careful when applying functions.
  • It can also implement user-defined functions on vectors or arrays.

1. Syntax of outer() Function

Following is the syntax of the outer() function in R programming.


# Syntax of outer() function
outer(X, Y, FUN = "*", …)

The following are parameters.

  • X – First argument; Array or Vector
  • Y – Second argument; Array or Vector
  • FUN – Function to apply on outer products. When not specified, it by default applies multiply

2. Outer() in R using Vector

Use the outer() function to get the outer product of the arrays or vectors in R. This function takes X and Y as arguments with dimensions dim(X) and dim(Y) and returns the matrix output c(dim(X), dim(Y)). In other words, the shape of the matrix would be (len(x), len(y)).


# Outer product of single Vector
x <- 1:6
y <- 2
res <- outer(x,y)
print(res)

Yields below output.

outer in r

3. R Outer Using Two Vectors

Now let’s check the output of the R outer product by using two vectors of the same size. Here, I am using the + operator as a function. Here, every element of the first vector is added to every element of the second vector and forms the matrix output.



# Outer product of two vectors
x <- 2:5
y <- 3:6
print(x)
print(y)
res <- outer(x,y, "+")
print(res)

Yields below output.

product

4. Outer in R with Custom Function

So far you have learned to use R outer() with the default function or use an arithmetic operator, besides these, you can also apply a custom operation by calling a user defined function. In the below example, I have created fun1() that takes the x and y arguments and applies the custom function.


# Outer with custom function
fun1 <- function(x,y){
  return ((x + y) * 2)
}
res2 <- outer(x,y, fun1)
print(res2)

Yields below output. Here, the outer() function calls the fun1() for every element of the first vector with every element of the second vector.

r outer function

5. Outer Product with Character Data Type

You can also use the R outer() function with character arrays or vectors and apply the custom function for each value.

Here, the paste() function is used to concatenate two character values with the separator "_". Note that even for character vectors the result would be a matrix. c() is used to create a character vector.


# Outer product of character vectors
x <- c('AB','CD')
y <- c('X','Y')
fun1 <- function(x,y){
  return (paste(x, y, sep="_"))
}
res2 <- outer(x,y, fun1)
print(res2)

Yields below output. As you see above, we created two character vectors, both consisting of letters.

r outer product

6. Conclusion

In this article, you have learned outer in R and using this how to product two arrays or vectors to get a Matrix by applying function. Also, learned to apply a function with outer() and finally use this function with character vectors.

You can find the complete example at GitHub Project.

Related Articles