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:
Python3
# 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:
Python3
# 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, "+" ) |
Output:
[, 1] [, 2] [, 3] [1, ] 3 5 7 [2, ] 4 6 8 [3, ] 5 7 9 [4, ] 6 8 10 [5, ] 7 9 11