Performing different Operations on Two Arrays in R Programming – outer() Function

outer() function in R 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

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate
# outer function
  
# Initializing two arrays of elements
x <- c(1, 2, 3, 4, 5)
y<- c(2, 4, 6)
  
# Adding array x elements with array y elements
outer(x, y, "+")

chevron_right


Output:

     [, 1] [, 2] [, 3]
[1, ]    3    5    7
[2, ]    4    6    8
[3, ]    5    7    9
[4, ]    6    8   10
[5, ]    7    9   11
My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.