outer() Function in R (4 Examples) | Apply User-Defined Function | Calculate Outer Product of Arrays

preview_player
Показать описание
R code of this video:

x1 <- 1:5 # Create x vector
y1 <- 3 # Create y value

output1 <- outer(x1, y1, "+") # Apply R outer function
output1 # Print output to RStudio console

x2 <- 1:5 # Create x vector (same as in Example 1)
y2 <- 3:7 # Create y vector

names(x2) <- x2 # Prepare row names of output matrix
names(y2) <- y2 # Prepare column names of output matrix

output2 <- outer(x2, y2, "+") # Apply R outer function
output2 # Print output to RStudio console

x3 <- letters[1:5] # Create x vector with characters
y3 <- LETTERS[1:5] # Create y vector with upper case

names(x3) <- x3 # Prepare row names of output matrix
names(y3) <- y3 # Prepare column names of output matrix

output3 <- outer(x3, y3, "paste") # Apply R outer function to characters
output3 # Print output to RStudio console

x4 <- 1:5 # Create x vector (same as in Example 1 & 2)
y4 <- 3:7 # Create y vector (same as in Example 2)

custom_fun <- function(x, y) { # Create custom function in R
z <- (x + y)^2
return(z)
}

names(x4) <- x4 # Prepare row names of output matrix
names(y4) <- y4 # Prepare column names of output matrix

output4 <- outer(x4, y4, custom_fun) # Apply R outer function to custom function
output4 # Print output to RStudio console

Follow me on Social Media:

Рекомендации по теме
join shbcf.ru