Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

outer() function in R

  • Last Updated : 09 Nov, 2021

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

outer() Function in R Programming Example

Example 1: Outer Product of Two Vector

R




# R program to illustrate
# outer function
  
# Initializing two arrays of elements
x <- c(1, 2, 3, 4, 5)
y<- c(2, 4, 6)
  
# Multiplying array x elements with array y elements
# Here multiply (*) parameter is not used still this 
# function take it as default
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

Example 2: outer Function for Vector and Single Value

R




# R program to illustrate
# outer function
  
# Initializing two arrays of elements
x <- 1:8
y<- 4
  
# Multiplying array x elements with array y elements
# Here multiply (*) parameter is not used still this 
# function take it as default
outer(x, y, "+")

Output: 

      [,1]
[1,]    5
[2,]    6
[3,]    7
[4,]    8
[5,]    9
[6,]   10
[7,]   11
[8,]   12
My Personal Notes arrow_drop_up
Recommended Articles
Page :