outer in R: How To Apply The Function To Two Vectors

In this article, we will share with you the usage, the syntax, and how to use the outer in R. The outer() function in R is used to help you apply the function to two vectors in R. Let’s learn more about it with the explanation and examples below.

Outer In R

What does the outer function do in R?

The outer() function helps you apply the function to two vectors in R. Besides the same mode, this function can also apply to two vectors that have different modes. And you can customize the function by yourself to apply it with two vectors. Let’s take a look at the syntax of the outer() function.

Syntax

outer(x, y, FUN = "*")

Parameters

  • x: The first vector.
  • y: The second vector.
  • FUN: The function you want to apply with two vectors. The default is multiplied. 

Return value

The result will be the matrix whose size is (len(x), len(y)).

After learning the usage and the syntax of the outer() function, you will learn how to use this function in the next title below.

How To Use The Outer In R

You can add, multiply, divide, or apply the specified function to two vectors by the outer() function in R.

Multiply two vectors with the outer function

You can multiply two vectors by the default syntax of the outer() function.

The first vector will multiply each element of the second vector in turn, and the result will be a matrix.

Look at the example below.

# The first vector.
vec1 <- c(1, 3, 5, 6, 7)

# The second vector
vec2 <- c(2, 3)

result <- outer(vec1, vec2)
result

Output

     [,1] [,2]
[1,]    2    3
[2,]    6    9
[3,]   10   15
[4,]   12   18
[5,]   14   21

Add two vectors with the outer function

You can add two vectors by assigning the value ‘+’ to the ‘FUN’ parameter.

The first vector will add each element of the second vector in turn, and the result will be a matrix.

Look at the example below.

# The first vector.
vec1 <- c(1, 3, 5, 6, 7)

# The second vector
vec2 <- c(2, 3)

result <- outer(vec1, vec2, FUN = "+")
result

Output

     [,1] [,2]
[1,]    3    4
[2,]    5    6
[3,]    7    8
[4,]    8    9
[5,]    9   10

Apply the custom function to two vectors

You can apply the custom function to two vectors by the outer() function.

Look at the example below.

# The first vector
vec1 <- c(1, 3, 5, 6, 7)

# The second vector
vec2 <- c(2, 3)

# Create the custom function
my_func <- function(a, b) {
    return(a + 3 * b)
}

# Apply the 'my_func' function to two vectors
result <- outer(vec1, vec2, FUN = my_func)
result

Output

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

Use the outer function with the character

You can apply the function to the two character vectors by assigning the value ‘paste’ to the ‘FUN’ parameter.

Look at the example below

# The first vector
vec1 <- c("a", "x", "Z")

# The second vector
vec2 <- c("Y", "k")

result <- outer(vec1, vec2, FUN = "paste")
result

Output

      [,1] [,2] 
[1,] "a Y" "a k"
[2,] "x Y" "x k"
[3,] "Z Y" "Z k"

Summary

We have shown you the definition, the syntax, and how to use the outer in R. You can use the outer() function in order to apply the function to two vectors in R. We hope this tutorial is helpful to you. Thanks!

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *