算术运算符更喜欢算术函数吗? [英] Are arithmetic operators ever preferable to arithmetic functions?

查看:124
本文介绍了算术运算符更喜欢算术函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



算术运算符:

>

 > `+`(4,5)
[1] 9

算术函数: p>

 > sum(4,5)
[1] 9

然而算术运算符没有

 > `+`(1:5)
[1] 1 2 3 4 5

有没有算术运算符比算术函数更适合的实例?

解决方案

对于标题中的问题,我会说..通常或通常。在R中,我们通常并行地处理列或向量,即,在作为同一个案例或主题的一部分的意义上,向量内的值的顺序。在你的例子中,你实际上看到了三个不同的函数:

第一:一元运算符'+'在给定数字向量时基本上什么也不做,但是会对数字进行强制转换当给出逻辑向量时:

 > `+`(-3:3)
[1] -3 -2 -1 0 1 2 3
> `+`(c(TRUE,FALSE))
[1] 1 0

它不会 使其所有返回值为正值,正如其中一条注释中所述。下一步:二进制+运算符:

 > `+`(-3:3,0:6)
[1] -3 -1 1 3 5 7 9

最后: sum 函数,它是_not_vectorized_,并将所有值折叠为它们的联合和:

 > sum(-3:3,0:6,c(TRUE,FALSE))
[1] 22

二元 + 也会回收参数(带有警告):

 > `+`(-3:3,c(TRUE,FALSE))
[1] -2 -2 0 0 2 2 4
警告信息:
In -3:3 + c (TRUE,FALSE):
对象的长度不是较短对象长度的倍数

当对并行向量进行操作时,您通常会想要使用向量化的运算符,并且对sum的结果感到非常失望,该结果只能返回长度为1的值。一些函数像 outer 依赖于函数参数,要求函数被矢量化。

 >外部(1:4,5:8,总和)
外部(1:4,5:8,总和)中的错误:
dims [product 16]与对象[1]
>外(1:4,5:8,+)
[,1] [,2] [,3] [,4]
[1,] 6 7 8 9
[2,] 7 8 9 10
[3,] 8 9 10 11
[4,] 9 10 11 12

其他需要返回单个值。还有其他一些像'mapply`这样的主题是不可知论的。

 > mapply(总和,1:4,5:8)
[1] 6 8 10 12
> mapply(+,1:4,5:8)
[1] 6 8 10 12

可以使用 Vectorize 创建一个非矢量化函数的版本,并在内部使用 mapply 返回一个不同的函数,但它仅限于非原始函数,因为它们不在它们的形式列表中使用参数名称(所以你不能矢量化 sum 。) p>

最后应该注意的是,通过查阅?可以得到R优先规则的优先顺序不同。语法 。 (一元算术运算符的优先级高于二进制算子,函数和括号隐含地具有最高的优先级,我已经看到由于用户定义的函数具有更高优先级而出现意外的情况。)


I've noticed there are some instances in which an arithmetic operator (in backticks) acts as if it is an arithmetic function.

Arithmetic operator:

> `+`(4, 5)
[1] 9

Arithmetic function:

> sum(4, 5)
[1] 9

Yet the arithmetic operator doesn't accomplish this across vectors like typical arithmetic functions:

> `+`(1:5)
[1] 1 2 3 4 5

Is there ever any instance in which the arithmetic operator is preferable to the arithmetic function?

解决方案

To the question in the title, I would say ..often or typically. In R we are often working on columns or vectors in parallel, i,e, the order of the values in a vector matter in the sense of being part of the same case or subject. In your examples you are actually seeing three different functions in action:

First: the unary '+' operator which basically does nothing when given numeric vectors but would do coercion to numeric when given logical vectors:

>  `+`(-3:3)
[1] -3 -2 -1  0  1  2  3
> `+`(c(TRUE,FALSE))
[1] 1 0

Notice that it does not make all of its returned values positive as was stated in one of the comments.

Next: the binary '+` operator:

> `+`(-3:3, 0:6)
[1] -3 -1  1  3  5  7  9

And finally: the sum function, which is _not_vectorized_ and will collapse all values to the sum of their union:

> sum(-3:3, 0:6, c(TRUE,FALSE) )
[1] 22

The binary + will also recycle arguments (with a warning):

> `+`(-3:3, c(TRUE,FALSE) )
[1] -2 -2  0  0  2  2  4
Warning message:
In -3:3 + c(TRUE, FALSE) :
  longer object length is not a multiple of shorter object length

When operating on vectors "in parallel" you will often want to use the vectorized operators and be very much disappointed by the results of sum which can only return a value of length 1. Some functions like outer which depend on a functional argument require that the function be vectorized.

>  outer(1:4, 5:8, sum)
Error in outer(1:4, 5:8, sum) : 
  dims [product 16] do not match the length of object [1]
> outer(1:4, 5:8, "+")
     [,1] [,2] [,3] [,4]
[1,]    6    7    8    9
[2,]    7    8    9   10
[3,]    8    9   10   11
[4,]    9   10   11   12

Others require that a single value be returned. And some others like 'mapply` are agnostic on the subject.

> mapply(sum, 1:4, 5:8)
[1]  6  8 10 12
> mapply("+", 1:4, 5:8)
[1]  6  8 10 12

The Vectorize can be used to create a version of a non-vectorized function and internally it is using mapply to return a different function, but it is limited to non-primitive functions since they do not use argument names in their formals list (so you cannot vectorize sum.)

It should probably be noted in closing that these have different precedence for which one can get R precedence rules by consulting ?Syntax. (The unary arithmetic operators have higher precedence than the binary ones. Functions and parentheses implicitly would have the highest precedence. I have seen situations where there were surprises due to the higher precedence of user defined functions.)

这篇关于算术运算符更喜欢算术函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆