Apply Function into two input vectors in R

Apply Function into two input vectors in R, Two arrays are subjected to a function by the R-outer function. Above is a basic example of R code for an outer command.

W’ll give you four examples of how to use outer in R in the ensuing tutorial.

Apply Function into two input vectors in R

Basic R Syntax:

outer(x, y, "*")

Example 1: Outer Function with Single Value and Vector

Applying outer on a numeric vector and a single value in R is arguably its most fundamental use. Let’s build this data in R for the first example:

x1 <- 3:8                                
y1 <- 2

Our example vector has the numbers 3 to 8, and we will use the number 2 as a single value.

With the following syntax, we can use this data to apply the R-outer function:

output1 <- outer(x1, y1, "+")             
output1 
      [,1]
[1,]    5
[2,]    6
[3,]    7
[4,]    8
[5,]    9
[6,]   10

The output of the outer function’s RStudio console is displayed in Table 1. As you can see, the numbers 3 + 2, 4 + 2, 5 + 2, 6 + 2, 7+2, and 8 + 2 were added to each entry of our sample vector.

Although I’ve used the plus function in this example, we could use any other function instead.

It was simple, right? Let’s continue with other instances that are more sophisticated.

Example 2: Outer Product of Two Vectors

The second example will demonstrate how to use R’s outer function on two numeric vectors (or arrays). Let’s first generate some sample data:

x2 <- 10:15                               
y2 <- 3:7
output <- outer(x2, y2, "+")            
output 
     [,1] [,2] [,3] [,4] [,5]
[1,]   13   14   15   16   17
[2,]   14   15   16   17   18
[3,]   15   16   17   18   19
[4,]   16   17   18   19   20
[5,]   17   18   19   20   21
[6,]   18   19   20   21   22

Additionally, you can see that our output matrix’s row and column names are tagged with the values of our input vectors. With the help of the small renaming trick I’ve previously shown you, we achieved this.

You can now navigate between the cells of the output matrix to see what each combination of our two vectors produced.

Example 3: Apply outer to several data types

We have just utilized numbers and vectors up to this point. But other data types can also be used with the outer R command. We’ll demonstrate how to utilize the outer command for characters in this example.

Best Data Science YouTube Tutorials Free to Learn – Data Science Tutorials

Let’s start over with some fresh information:

x3 <- letters[1:5]                       
y3 <- LETTERS[1:5]

You can see that we made two character vectors with the letters a, b, c, d, and e in each. In the instance of the second vector, we employ uppercase letters for illustration purposes.

The exact same R syntax as in the earlier examples is now available to us. In order to alter the column names of our output matrix, let’s first use our little trick.

names(x3) <- x3                          
names(y3) <- y3                          
out3 <- outer(x3, y3, "paste")
out3              
     A     B     C     D     E   
a "a A" "a B" "a C" "a D" "a E"
b "b A" "b B" "b C" "b D" "b E"
c "c A" "c B" "c C" "c D" "c E"
d "d A" "d B" "d C" "d D" "d E"
e "e A" "e B" "e C" "e D" "e E"

same outcome as previously, but with characters this time. The output matrix stores each possible combination of our two vectors.

Because it would be meaningless for letters, we did not use the + function for our two vectors this time. Instead, we combined the character values from our two vector strings using the paste method.

When to use Kruskal Wallis Test »

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

4 + 16 =