In this tutorial, we will learn how to use outer() function in R. The outer() function in R is a useful tool for applying a function to all pairs of elements of two input vectors.
The basic syntax of using outer() function is this
outer(X, Y, FUN ="*")
Here X and Y are vectors and default function to the argument is multiplication.
And one can learn more about the features of outer() function by getting the help page as shown below.
?outer
outer() function example 1
To illustrate the use of outer, let’s consider a simple example where we want to multiply all pairs of elements from two input vectors. Here the function we are applying is the multiplication function. Here is how we could do this using outer:
# Let us Define the input vectors x <- c(1, 2, 3) y <- c(4, 5, 6)
# Define the function to apply to all pairs of elements multiply <- function(a, b) { return(a * b) }
# Apply the function to all pairs of elements using outer result <- outer(x, y, multiply)
And we will get a matrix containing multiplication of all pairs . The output matrix is of dimensions 3 x 3, where each element is the product of the corresponding elements from the input vectors. For example, the element at row 1, column 1 is the product of the first elements of x and y, which is 1 * 4 = 4. The element at row 2, column 3 is the product of the second element of x and the third element of y, which is 2 * 6 = 12.
result [,1] [,2] [,3] [1,] 4 5 6 [2,] 8 10 12 [3,] 12 15 18
outer() function example 2
As we said earlier, the default function for outer function is multiplication function, we don’t have specify a function if we want to multiply all pairs of elements from two vectors.
And R has a wrapper for outer function and it is denoted with aa symbol %o%. Here is. an example of using %o% to get multiplication/power tables for numbers 1 to 9.
x <- 1:9 names(x) <- x
# Multiplication & Power Tables x %o% x ## 1 2 3 4 5 6 7 8 9 ## 1 1 2 3 4 5 6 7 8 9 ## 2 2 4 6 8 10 12 14 16 18 ## 3 3 6 9 12 15 18 21 24 27 ## 4 4 8 12 16 20 24 28 32 36 ## 5 5 10 15 20 25 30 35 40 45 ## 6 6 12 18 24 30 36 42 48 54 ## 7 7 14 21 28 35 42 49 56 63 ## 8 8 16 24 32 40 48 56 64 72 ## 9 9 18 27 36 45 54 63 72 81
outer() function example 3
Here is another use of outer function to create all possible combinations two elements of a vector. Our vector is a vector of colors and we apply paste() function in R to combine two colors.
colors <- c("red", "orange", "yellow", "green", "blue", "purple") combinations <- outer(colors, colors, paste)
This code creates a matrix of all possible combinations of two colors in the vector “colors”. The matrix contains one row for each possible combination, with the first and second elements of each row representing the two colors being combined.
combinations [,1] [,2] [,3] [,4] [1,] "red red" "red orange" "red yellow" "red green" [2,] "orange red" "orange orange" "orange yellow" "orange green" [3,] "yellow red" "yellow orange" "yellow yellow" "yellow green" [4,] "green red" "green orange" "green yellow" "green green" [5,] "blue red" "blue orange" "blue yellow" "blue green" [6,] "purple red" "purple orange" "purple yellow" "purple green" [,5] [,6] [1,] "red blue" "red purple" [2,] "orange blue" "orange purple" [3,] "yellow blue" "yellow purple" [4,] "green blue" "green purple" [5,] "blue blue" "blue purple" [6,] "purple blue" "purple purple"
outer() function example 4
In the fourth example on the use of outer() function, we create a matrix containing month and year combinations for all months in the years 2020 to 2023.
We use R’s month.abb that contains short names of all months and use paste function to combine month and year
month.abb outer(month.abb, 2020:2023, FUN = paste) [,1] [,2] [,3] [,4] [1,] "Jan 2020" "Jan 2021" "Jan 2022" "Jan 2023" [2,] "Feb 2020" "Feb 2021" "Feb 2022" "Feb 2023" [3,] "Mar 2020" "Mar 2021" "Mar 2022" "Mar 2023" [4,] "Apr 2020" "Apr 2021" "Apr 2022" "Apr 2023" [5,] "May 2020" "May 2021" "May 2022" "May 2023" [6,] "Jun 2020" "Jun 2021" "Jun 2022" "Jun 2023" [7,] "Jul 2020" "Jul 2021" "Jul 2022" "Jul 2023" [8,] "Aug 2020" "Aug 2021" "Aug 2022" "Aug 2023" [9,] "Sep 2020" "Sep 2021" "Sep 2022" "Sep 2023" [10,] "Oct 2020" "Oct 2021" "Oct 2022" "Oct 2023" [11,] "Nov 2020" "Nov 2021" "Nov 2022" "Nov 2023" [12,] "Dec 2020" "Dec 2021" "Dec 2022" "Dec 2023"