This is a very basic question - but apparently google is not very good at searching for strings like "%+%". So my question is - what and when is "%+%" and similar used. I guess its a kind of merge?.
这是一个非常基本的问题——但是显然谷歌不太擅长搜索像“%+%”这样的字符串。所以我的问题是-什么时候用“%+%”和类似的词。我猜这是一种归并。
EDIT: Ok - I believe my question is answered. %X% is binary operator of some kind. So now I think I will google around for knowledge about how/when to use these. My question was partly inspired by yesterday's question - but only after I saw this post on the "learning R" blog. The passage that gave rise to my question was this:
In order to do this, a new dataframe with the annual totals will be created and later merged with the existing dataset (variable names in both dataframes should be identical for this to work). Then we just change the dataframe the plot is based on.
编辑:好的,我想我的问题已经得到了回答。%X%是某种二进制运算符。所以现在我想我要在谷歌上了解如何/何时使用它们。我的问题在一定程度上受到了昨天问题的启发,但这是在我看到“学习R”博客上的这篇文章之后。引起我这个问题的文章是这样的:为了做到这一点,将创建一个新的具有年度总数的数据aframe,然后与现有的数据集合并(两个dataframes中的变量名应该与现有的数据集一致)。然后我们只需改变这个图的数据名。
## add total immigration figures to the plot
total <- cast(df.m, Period ~ ., sum)
total <- rename(total, c("(all)" = "value"))
total$Region <- "Total"
df.m.t <- rbind(total, df.m)
c1 <- c %+% df.m.t
3 个解决方案
#1
24
The ultimate reason is that if you do both general-purpose programming and numerical computations, it is useful to have a large complement of binary operators available. For example, if you store numbers in two-dimensional arrays, you may want to multiply the arrays elementwise, or you may want to compute the matrix product of two arrays. In Matlab these two operators are .*
and *
; in R they are *
and %*%
. Python has resisted attempts to add new operators, and so numpy differentiates between the two kinds of product by having two classes: the array class is multiplied elementwise, the matrix class is multiplied in the linear-algebra sense.
最终的原因是,如果您同时进行通用编程和数值计算,那么拥有大量二进制运算符是有用的。例如,如果您将数字存储在二维数组中,您可能希望将数组元素相乘,或者您可能希望计算两个数组的矩阵乘积。在Matlab中,这两个运算符是。*和*;在R中它们是*和%*%。Python一直拒绝添加新的运算符,并且通过两个类来区分这两类产品:数组类的元素相乘,矩阵类在线性代数的意义上相乘。
Another example from Python is that for lists, plus means concatenation: [1,2,3]+[4,5] == [1,2,3,4,5]
. But for numpy arrays, plus means elementwise addition: array([1,2]) + array([4,5]) == array([5,7])
. If your code needs to do both, you have to convert between classes or use function notation, which can lead to cumbersome-looking code, especially where mathematics is involved.
Python的另一个例子是列表,加的意思是串联:[1,2,3]+[4,5]==[1,2,3,4,5]。但是对于numpy数组,plus意味着元素相加:array([1,2]) + array([4,5]) = array([5,7])。如果您的代码需要同时做这两件事,那么您必须在类之间进行转换或使用函数表示法,这可能会导致看起来冗长的代码,特别是涉及到数学的代码。
So it would sometimes be convenient to have more operators available for use, and you might not know in advance what sorts of operators a particular application calls for. Therefore, the implementors of R have chosen to treat as operators anything named like %foo%
, and several examples exist: %in%
is set membership, %x%
is Kronecker product, %o%
is outer product. For an example of a language that has taken this to the extreme, see Fortress (section 16 of the specification starts with the rules for operator names).
因此,有时使用更多的运算符是很方便的,而且您可能事先不知道特定应用程序需要哪种运算符。因此,R的实现者选择将任何名称为%foo%的操作符视为操作符,有几个例子:%中的%是集合成员,%x%是Kronecker产品,%o%是外部产品。对于一个将其带到极端的语言的示例,参见要塞(规范的第16节以操作符名称的规则开始)。
In the blog post you mentioned, the author is using the ggplot2 graphing package, which defines %+%
to mean some kind of combination of two plot elements. Really it seems to add a method to the bare +
(which is a generic function so you can define what it means for user-defined objects), but it also defines %+%
so that you can use the ggplot2 meaning of +
(whatever it is) for other objects. If you install ggplot2, type require(ggplot2)
and ?`%+%`
to see the documentation of that operator, and methods(`+`)
to see that a new definition has been added to +
.
在您提到的博客文章中,作者使用了ggplot2图形包,该包定义了%+%,表示两个情节元素的某种组合。实际上,它似乎向bare +(这是一个通用函数,可以定义它对用户定义的对象意味着什么)添加了一个方法,但是它也定义了%+%,以便您可以对其他对象使用ggplot2的含义(无论它是什么)。如果您安装了ggplot2,那么输入require(ggplot2)和?
#2
15
There is no generally defined %+%
. Maybe you looked at this question from yesterday where
没有一般定义的%+%。也许你昨天在哪里看过这个问题
R> '%+%' <- paste
R> "foo" %+% "bar"
[1] "foo bar"
R>
and ad-hoc string concatenation function was defined. Generally, the 'percent-operator-percent' syntax is open for user-defined functions of two arguments, but there is (AFAIK) no generally accepted version for %+%
that you can expect to be present everywhere.
定义了一种特殊的字符串连接函数。一般来说,对于两个参数的用户定义函数,“百分比-操作数-百分比”语法是开放的,但是(AFAIK)没有普遍接受的%+%的版本,您可以期望在任何地方出现。
#3
1
Based on my quick look at the manual it may be a user defined infix operator, so, it's hard to tell what the actual meaning would be...
根据我对手册的快速了解,它可能是一个用户定义的中缀操作符,因此,很难说它的真正含义是什么……
I would think binary addition.
我认为是二进制加法。
#1
24
The ultimate reason is that if you do both general-purpose programming and numerical computations, it is useful to have a large complement of binary operators available. For example, if you store numbers in two-dimensional arrays, you may want to multiply the arrays elementwise, or you may want to compute the matrix product of two arrays. In Matlab these two operators are .*
and *
; in R they are *
and %*%
. Python has resisted attempts to add new operators, and so numpy differentiates between the two kinds of product by having two classes: the array class is multiplied elementwise, the matrix class is multiplied in the linear-algebra sense.
最终的原因是,如果您同时进行通用编程和数值计算,那么拥有大量二进制运算符是有用的。例如,如果您将数字存储在二维数组中,您可能希望将数组元素相乘,或者您可能希望计算两个数组的矩阵乘积。在Matlab中,这两个运算符是。*和*;在R中它们是*和%*%。Python一直拒绝添加新的运算符,并且通过两个类来区分这两类产品:数组类的元素相乘,矩阵类在线性代数的意义上相乘。
Another example from Python is that for lists, plus means concatenation: [1,2,3]+[4,5] == [1,2,3,4,5]
. But for numpy arrays, plus means elementwise addition: array([1,2]) + array([4,5]) == array([5,7])
. If your code needs to do both, you have to convert between classes or use function notation, which can lead to cumbersome-looking code, especially where mathematics is involved.
Python的另一个例子是列表,加的意思是串联:[1,2,3]+[4,5]==[1,2,3,4,5]。但是对于numpy数组,plus意味着元素相加:array([1,2]) + array([4,5]) = array([5,7])。如果您的代码需要同时做这两件事,那么您必须在类之间进行转换或使用函数表示法,这可能会导致看起来冗长的代码,特别是涉及到数学的代码。
So it would sometimes be convenient to have more operators available for use, and you might not know in advance what sorts of operators a particular application calls for. Therefore, the implementors of R have chosen to treat as operators anything named like %foo%
, and several examples exist: %in%
is set membership, %x%
is Kronecker product, %o%
is outer product. For an example of a language that has taken this to the extreme, see Fortress (section 16 of the specification starts with the rules for operator names).
因此,有时使用更多的运算符是很方便的,而且您可能事先不知道特定应用程序需要哪种运算符。因此,R的实现者选择将任何名称为%foo%的操作符视为操作符,有几个例子:%中的%是集合成员,%x%是Kronecker产品,%o%是外部产品。对于一个将其带到极端的语言的示例,参见要塞(规范的第16节以操作符名称的规则开始)。
In the blog post you mentioned, the author is using the ggplot2 graphing package, which defines %+%
to mean some kind of combination of two plot elements. Really it seems to add a method to the bare +
(which is a generic function so you can define what it means for user-defined objects), but it also defines %+%
so that you can use the ggplot2 meaning of +
(whatever it is) for other objects. If you install ggplot2, type require(ggplot2)
and ?`%+%`
to see the documentation of that operator, and methods(`+`)
to see that a new definition has been added to +
.
在您提到的博客文章中,作者使用了ggplot2图形包,该包定义了%+%,表示两个情节元素的某种组合。实际上,它似乎向bare +(这是一个通用函数,可以定义它对用户定义的对象意味着什么)添加了一个方法,但是它也定义了%+%,以便您可以对其他对象使用ggplot2的含义(无论它是什么)。如果您安装了ggplot2,那么输入require(ggplot2)和?
#2
15
There is no generally defined %+%
. Maybe you looked at this question from yesterday where
没有一般定义的%+%。也许你昨天在哪里看过这个问题
R> '%+%' <- paste
R> "foo" %+% "bar"
[1] "foo bar"
R>
and ad-hoc string concatenation function was defined. Generally, the 'percent-operator-percent' syntax is open for user-defined functions of two arguments, but there is (AFAIK) no generally accepted version for %+%
that you can expect to be present everywhere.
定义了一种特殊的字符串连接函数。一般来说,对于两个参数的用户定义函数,“百分比-操作数-百分比”语法是开放的,但是(AFAIK)没有普遍接受的%+%的版本,您可以期望在任何地方出现。
#3
1
Based on my quick look at the manual it may be a user defined infix operator, so, it's hard to tell what the actual meaning would be...
根据我对手册的快速了解,它可能是一个用户定义的中缀操作符,因此,很难说它的真正含义是什么……
I would think binary addition.
我认为是二进制加法。