当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R语言 outer()用法及代码示例


R 语言中的 outer() 函数用于将函数应用于两个数组。

用法: outer(x, y, FUN=”*”, …)
参数: 
x, y: arrays 
FUN: function to use on the outer products, default value is multiply 
 

范例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)

输出:



     [, 1] [, 2] [, 3]
[1, ]    2    4    6
[2, ]    4    8   12
[3, ]    6   12   18
[4, ]    8   16   24
[5, ]   10   20   30

范例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, "+")

输出:

     [, 1] [, 2] [, 3]
[1, ]    3    5    7
[2, ]    4    6    8
[3, ]    5    7    9
[4, ]    6    8   10
[5, ]    7    9   11

相关用法


注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 Performing different Operations on Two Arrays in R Programming – outer() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。