例如,%+%会做什么?在R中 [英] What does eg %+% do? in R

查看:142
本文介绍了例如,%+%会做什么?在R中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常基本的问题-但显然Google不太擅长搜索%+%"之类的字符串.所以我的问题是-什么时候使用%+%"及类似内容.我想这是一种合并吗?.

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?.

好的-我相信我的问题已经回答. %X%是某种二进制运算符.所以现在我想我将在Google周围搜索有关如何/何时使用这些知识.我的问题部分地受到了昨天的问题的启发-但直到我看到此帖子在学习R"博客上.引起我疑问的段落是这样的:
为此,将创建一个具有年度总计的新数据框,然后将其与现有数据集合并(两个数据框中的变量名称应相同,才能正常工作).然后我们只需更改绘图所基于的数据框即可.

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.

## 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

推荐答案

最终的原因是,如果您同时进行通用编程和数值计算,那么拥有大量可用的二进制运算符将很有用.例如,如果将数字存储在二维数组中,则可能需要逐个将数组相乘,或者可能要计算两个数组的矩阵乘积.在Matlab中,这两个运算符是.**;在R中,它们是*%*%. Python具有抵抗 numpy 通过具有两个类别来区分两种乘积:数组类别按元素相乘,矩阵类别按线性代数意义相乘.

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.

Python的另一个示例是用于列表,加号表示串联:[1,2,3]+[4,5] == [1,2,3,4,5].但是对于numpy数组,加号表示按元素加法:array([1,2]) + array([4,5]) == array([5,7]).如果您的代码需要同时执行这两项操作,则必须在类之间进行转换或使用函数表示法,这可能会导致代码看起来笨拙,尤其是在涉及数学的情况下.

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.

因此有时可以使用更多运算符会很方便,并且您可能事先不知道特定应用程序需要什么样的运算符.因此,R的实现者选择将名称为%foo%的任何内容视为运算符,并且存在几个示例:%in%是集合成员资格,%x%是Kronecker乘积,%o%是外部乘积.有关将语言发挥到极致的示例,请参见要塞(规范的第16节以操作员姓名的规则开头).

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).

在您提到的博客文章中,作者使用的是 ggplot2 图形软件包,该软件包定义了%+%表示两种情节元素的某种组合.确实似乎在裸+上添加了一个方法(这是一个泛型函数,因此您可以定义它对用户定义的对象的含义),但是它也定义了%+%,以便您可以使用ggplot2的含义+(无论是什么)用于其他对象.如果安装ggp​​lot2,请键入require(ggplot2)?`%+%`以查看该运算符的文档,并键入methods(`+`)以查看是否已向+添加新定义.

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 +.

这篇关于例如,%+%会做什么?在R中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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