In R, the **outer()** function runs a function on two vectors, creating a result matrix. The generic syntax for using the **outer()** function is as follows:

outer(x, y, FUN=function(a, b) { statements })

Where **x** and **y** are vectors (or scalars) and the **FUN** is a function that is called on each combination of the elements in the two input vectors.

**This is a comprehensive guide to using the outer() function in R.** You will learn the usage of the **outer()** function through common example use cases with detailed explanations and theory.

## What Is the outer() Function in R?

Dealing with arrays is commonplace when writing R programs and doing data science. Having the ability to **perform an operation between the array elements** is a frequent necessity for data scientists.

In R, there’s a built-in **outer()** function you can call on two vectors (and scalars) to perform an operation between the element combinations of the vectors to create a result matrix.

Here’s the syntax for calling the **outer()** function:

outer(x, y, FUN=function(a, b) { statements })

Where:

**x**is a vector or array (or a scalar)**y**is another vector or array (or a scalar)**FUN**is a function you perform on the elements of the input vectors. The function runs on each element combination between the vectors**x**and**y**. The function must take two arguments and return a value.

The function **FUN** can also use symbols, such as “+” or “*” besides being a separately defined function.

For instance, let’s use the **outer()** function to add a number to each element of a vector:

x <- 1:10 y <- 5 output1 <- outer(x, y, "+") output1

Output:

[,1] [1,] 6 [2,] 7 [3,] 8 [4,] 9 [5,] 10 [6,] 11 [7,] 12 [8,] 13 [9,] 14 [10,] 15

Now that you have a basic understanding of how the **outer()** function works, let’s take a look at some common use cases of it. The following examples include calling the **outer()** function on **vectors**, creating **custom functions**, as well as using **non-numeric data types**.

### 1. Call outer() Function on Vectors and Scalars

As a first example, let’s call the **outer()** function on a vector and a scalar. More specifically, let’s create a vector in which you add **5** to each element.

Here’s what the code looks like:

x <- 1:10 y <- 5 output1 <- outer(x, y, "+") output1

Output:

[,1] [1,] 6 [2,] 7 [3,] 8 [4,] 9 [5,] 10 [6,] 11 [7,] 12 [8,] 13 [9,] 14 [10,] 15

Here’s an illustration of how the outer() function operates between the scalar 5 and the vector 1:10:

As stated earlier, the function **FUN** in the **outer()** function call takes each combination of the input argument vectors and performs the function on them.

Because in the above example, the input was a vector and a scalar, the addition operation took place for each element in the vector producing a new vector.

### 2. Call outer() Function on Two Vectors

Now, let’s continue with a very similar example to the first one but instead of a vector and a scalar, let’s operate on two vectors.

Let’s create a matrix of element-wise sums for two vectors from 1 to 10.

Here’s how it looks in the code:

x <- 1:10 y <- 1:10 result <- outer(x, y, "+") result

Output:

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 2 3 4 5 6 7 8 9 10 11 [2,] 3 4 5 6 7 8 9 10 11 12 [3,] 4 5 6 7 8 9 10 11 12 13 [4,] 5 6 7 8 9 10 11 12 13 14 [5,] 6 7 8 9 10 11 12 13 14 15 [6,] 7 8 9 10 11 12 13 14 15 16 [7,] 8 9 10 11 12 13 14 15 16 17 [8,] 9 10 11 12 13 14 15 16 17 18 [9,] 10 11 12 13 14 15 16 17 18 19 [10,] 11 12 13 14 15 16 17 18 19 20

Now, I’m not going to draw an illustration of this function because the result would be a mess. But the above code works like the first example. Now, the function just takes each element in the vector **y** and sums it up with each element in the vector **x**, forming a **10×10** matrix of element-wise sums.

### 3. Custom Function Argument

Thus far, you’ve only seen the **outer()** function with an input function of “+”. But this function can be anything else, as long as it takes two elements as arguments and returns a result.

#### Example 1

For example, let’s create a custom function **powerize** that raises input argument **a** to the **b**th power. Then, let’s call the **powerize** function for each element pair between two vectors **x** and **y**:

x <- 1:3 y <- 1:5 powerize <- function(a, b) { a^b } result <- outer(x, y, powerize) result

Output:

[,1] [,2] [,3] [,4] [,5] [1,] 1 1 1 1 1 [2,] 2 4 8 16 32 [3,] 3 9 27 81 243

#### Example 2

As another example, let’s create a colorful plot by creating a grid of values** cos(a*b)**, where **a** and **b** are values of uniformly distributed vectors between values **-1** and **1**.

x <- y <- seq(-1, 1, len=20); grid <- outer(x, y, function(a, b) { cos(a * b) }); filled.contour(grid, color.palette=rainbow);

Output:

### 4. Using outer() For Different Data Types

Thus far, you’ve seen examples of calling the **outer()** function on numbers. But it’s good to realize you’re not only restricted to using numbers with the **outer()** function. You can perform operations on other data types, such as lists of letters.

For example, let’s create a table of letter combinations given two groups of letters:

x <- LETTERS[1:8] y <- letters[1:5] result <- outer(x, y, "paste") result

Output:

[,1] [,2] [,3] [,4] [,5] [1,] "A a" "A b" "A c" "A d" "A e" [2,] "B a" "B b" "B c" "B d" "B e" [3,] "C a" "C b" "C c" "C d" "C e" [4,] "D a" "D b" "D c" "D d" "D e" [5,] "E a" "E b" "E c" "E d" "E e" [6,] "F a" "F b" "F c" "F d" "F e" [7,] "G a" "G b" "G c" "G d" "G e" [8,] "H a" "H b" "H c" "H d" "H e"

Here the **outer()** function takes the two sequential lists of letters and creates a table of combinations of the letters in the vectors.

**Make sure to also read my guide to the seq() function in R.**

## Summary

Today you learned how to use the built-in **outer()** function in R.

To take home, the** outer()** function performs an operation between two vectors by running a function on all combinations of vector elements. The **outer()** function takes three arguments **x**, **y**, and **FUN**, where:

**x**is the first input vector.**y**is the second input vector.**FUN**is a function that performs an operation for each combination of element pairs from**x**and**y**vectors.

Thanks for reading. Happy coding!

## Read Also

Best Python Data Science Courses

#### About the Author

- I'm an entrepreneur and a blogger from Finland. My goal is to make coding and tech easier for you with comprehensive guides and reviews.

##### Recent Posts

- Artificial Intelligence2023.03.14Best AI Tools of 2023 (Save Time & Boost Productivity)
- Python2023.02.16How to Pass a Variable from HTML Page to Another (w/ JavaScript)
- JavaScript2023.02.16JavaScript How to Get Selected Text from a Textbox
- JavaScript2023.02.15JavaScript Select All Text in Div with a Mouse Click