Built with Tutify.io

    AIStudyLabs

    outer() Function in R with Examples

    outer() Function in R with Examples

    The outer() in R is used to apply the function to two arrays or vectors to get the result in a Matrix. In this article, I will explain the syntax of the outer() function and how to use this to get the product of two arrays or vectors by applying custom functions.

    R outer() Function Key Points

    <a id="h-1-syntax-of-outer-function"></a>

    1. Syntax of outer() Function

    Following is the syntax of the outer() function in R programming.

    
    # Syntax of outer() function
    outer(X, Y, FUN = "*", …)
    

    The following are parameters.

    2. Outer() in R using Vector

    Use the outer() function to get the outer product of the arrays or vectors in R. This function takes X and Y as arguments with dimensions dim(X) and dim(Y) and returns the matrix output c(dim(X), dim(Y)). In other words, the shape of the matrix would be (len(x), len(y)).

    
    # Outer product of single Vector
    x <- 1:6
    y <- 2
    res <- outer(x,y)
    print(res)
    

    Yields below output.

    3. R Outer Using Two Vectors

    Now let's check the output of the R outer product by using two vectors of the same size. Here, I am using the + operator as a function. Here, every element of the first vector is added to every element of the second vector and forms the matrix output.

    
    # Outer product of two vectors
    x <- 2:5
    y <- 3:6
    print(x)
    print(y)
    res <- outer(x,y, "+")
    print(res)
    

    Yields below output.

    4. Outer in R with Custom Function

    So far you have learned to use R outer() with the default function or use an arithmetic operator, besides these, you can also apply a custom operation by calling a user defined function. In the below example, I have created fun1() that takes the x and y arguments and applies the custom function.

    
    # Outer with custom function
    fun1 <- function(x,y){
      return ((x + y) * 2)
    }
    res2 <- outer(x,y, fun1)
    print(res2)
    

    Yields below output. Here, the outer() function calls the fun1() for every element of the first vector with every element of the second vector.

    5. Outer Product with Character Data Type

    You can also use the R outer() function with character arrays or vectors and apply the custom function for each value.

    Here, the paste() function is used to concatenate two character values with the separator "_". Note that even for character vectors the result would be a matrix. c() is used to create a character vector.

    
    # Outer product of character vectors
    x <- c('AB','CD')
    y <- c('X','Y')
    fun1 <- function(x,y){
      return (paste(x, y, sep="_"))
    }
    res2 <- outer(x,y, fun1)
    print(res2)
    

    Yields below output. As you see above, we created two character vectors, both consisting of letters.

    6. Conclusion

    In this article, you have learned outer in R and using this how to product two arrays or vectors to get a Matrix by applying function. Also, learned to apply a function with outer() and finally use this function with character vectors.

    You can find the complete example at GitHub Project.

    Related Articles