A flexible tool for working with matrices and vectors in R is the outer() function. It enables you to create a new matrix or array by applying a function to every conceivable combination of the items from two input vectors. outer() function in R Programming Language is used to apply a function to two arrays.
Syntax:
outer(X, Y, FUN = “*”)
Parameters:
- x, y: arrays
- FUN: function to use on the outer products, default value is multiply
A matrix or array with the same dimensions as the outer product of X and Y is what the outer() function in R produces. The function FUN is applied to the relevant pair of elements from X and Y to produce each element of the result.
Example 1: Outer Product of Two Vector
R
# Initializing two arrays of elements
x <- c(1, 2, 3, 4, 5)
y<- c(2, 4, 6)
outer(x, y)
Output:
[, 1] [, 2] [, 3]
[1, ] 2 4 6
[2, ] 4 8 12
[3, ] 6 12 18
[4, ] 8 16 24
[5, ] 10 20 30
Explanation: Multiplying array x elements with array y elements, Here multiply (*) parameter is not used still this function take it as default
Example 2: outer Function for Vector and Single Value
R
x <- 1:8
y<- 4
outer(x, y, "+")
Output:
[,1]
[1,] 5
[2,] 6
[3,] 7
[4,] 8
[5,] 9
[6,] 10
[7,] 11
[8,] 12
Explanation: Multiplying array x elements with array y elements, Here multiply (*) parameter is not used still this function take it as default
Types of outer() Functions
Since the outer() method is general, you can create your own unique functions and utilize them in conjunction with outer(). The following are a few of the more typical outer() function types:
1. Arithmetic Functions
Arithmetic operations such as addition, subtraction, multiplication, division, and modular operations are common use cases of the outer() function.
Example: Adding Elements from Two Vectors
R
x <- 1:3
y <- 4:6
outer(x, y, FUN = "+")
Output:
[,1] [,2] [,3]
[1,] 5 6 7
[2,] 6 7 8
[3,] 7 8 9
2. Statistical Functions
The outer() function can also be used with statistical functions to perform operations like calculating the correlation between two vectors or applying other statistical measures.
Example: Matrix Multiplication Using outer()
R
# Creating two matrices
X <- matrix(1:6, nrow = 2, ncol = 3)
Y <- matrix(1:4, nrow = 2, ncol = 2)
# Trying to multiply the two matrices using the outer function
outer(X, Y, "*")
Output:
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
, , 2, 1
[,1] [,2] [,3]
[1,] 2 6 10
[2,] 4 8 12
, , 1, 2
[,1] [,2] [,3]
[1,] 3 9 15
[2,] 6 12 18
, , 2, 2
[,1] [,2] [,3]
[1,] 4 12 20
[2,] 8 16 24
Similar Reads
Mutate function in R
The mutate() function in R Programming Language is used to add new variables in a data frame which are formed by performing operations on existing variables. It can be used by loading the dplyr library. Syntax: mutate(x, expr) Parameters: x: Data Frame expr: operation on variables Types of mutate()
3 min read
as.numeric() Function in R
The as.numeric() function in R is a crucial tool for data manipulation, allowing users to convert data into numeric form, which is essential for performing mathematical operations and statistical analysis. Overview of the as.numeric() FunctionThe as. numeric() function is part of R's base package an
3 min read
parse() Function in R
The parse() function in R programming language is used to return the parsed but unevaluated expression of a given expression in an expression, a âlistâ of calls. Also, this function converts an R object of the character class to an R object of the expression class. Syntax: parse(file = "", n = NULL,
2 min read
sum() function in R
sum() function in R Programming Language returns the addition of the values passed as arguments to the function. Syntax: sum(...) Parameters: ...: numeric or complex or logical vectorssum() Function in R ExampleR program to add two numbersHere we will use sum() functions to add two numbers. [GFGTABS
2 min read
map() Function in R
In R Programming Language the Map function is a very useful function used for element-wise operations across vectors or lists. This article will help show how to use it with multiple code examples. Map Function in RThe Map function in R belongs to the family of apply functions, designed to make oper
3 min read
Interactive() Function in R
In this article, we are going to see interactive() Function in R Programming Language. Interactive() Function It returns TRUE when R is being used interactively and FALSE otherwise. This function is used to test whether R runs interactively or not. Syntax: interactive() It will return TRUE, if is ru
1 min read
which() Function in R
which() function in R Programming Language is used to return the position of the specified values in the logical vector. Syntax: which(x, arr.ind, useNames) Parameters: This function accepts some parameters which are illustrated below: X: This is the specified input logical vectorArr.ind: This param
3 min read
by() Function in R
R has gained popularity for statistical computing and graphics. It provides the means of shifting the raw data into readable final results. in this article, we will discuss what is by() Function in R and how to use this. What is by() Function in R?The by() function is a localized function in R Progr
5 min read
Slice() Function In R
Slice() is a function in R Programming Language that is used to manipulate data frames and datasets using a simple syntax. It is used to make subsets of data frames, it allows data manipulation. These datasets can be sliced using the slice() function. R slice() function syntax:Syntax : slice(.data,
8 min read
Unique() Function in R
Unique() function in R Programming Language it is used to return a vector, data frame, or array without any duplicate elements/rows. Syntax: unique(x, incomparables, fromLast, nmax, â¦,MARGIN) Parameters: This function accepts some parameters which are illustrated below: x: This parameter is a vector
4 min read