为什么不"+"对R中的字符进行操作? [英] Why doesn't "+" operate on characters in R?

查看:56
本文介绍了为什么不"+"对R中的字符进行操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

叫我懒,但我一直讨厌一直输入paste("a","b",sep='')之类的东西.

Call me lazy, but I just hate typing things like paste("a","b",sep='') all the time.

我知道他是R.没有,只有那样." (library(fortunes);(fortune(109)).因此,我的后续问题是:是否可以轻松更改此行为?

I know that "(t)his is R. There is no if, only how." (library(fortunes);(fortune(109)). So, my follow up question is: Is it possible to easily change this behavior?

推荐答案

@ Dirk:有一次,您不太正确.不是解析器. 一种可以在R中为"+"编写方法-help("+")转到算术运算符"并提到 这些都是通用的,您可以为它们编写方法...当然,许多软件包编写者都可以这样做,例如,我们对'Matrix'软件包进行处理,而我也对"Rmpfr"软件包进行处理,例如 但是Dirk也是正确的(当然!),您目前无法在R中做到这一点, 只需为"+ .character"定义一个方法即可.

@ Dirk: For once, you're not quite right. It's not the parser. One can write methods in R for "+" -- help("+") goes to "Arithmetic operators" and mentions that these are generic and you can write methods for them ... and of course many package writers do, e.g., we do for the 'Matrix' package, and I also do for the "Rmpfr" package, e.g. But Dirk is also right (of course!) that you cannot do it in R currently, by just defining a method for "+.character".

大约三年前,我在R-devel(R开发的R邮件列表)上启动了一个线程;如果您对这些主题感兴趣,强烈建议;如果您不想通过Gmane访问,也可以订阅): r-devel归档的味精

About three years ago, I had started a thread on R-devel (the R mailing list on R development; very much recommended if you are interested in these topics; you can also access through Gmane if you don't want to subscribe) :r-devel archived msg

进行了有趣的讨论,其中有很多利弊, 尤其是John Chambers("S的父亲,因此是R"的父亲)强烈反对将"+"用于可交换的运算, 以及 r-devel已归档msg2 (另一位R-core成员),支持我们(R Core)不应采纳/支持该想法的观点;如果人们真的是想要的,他们可以定义 %+%.

It came to an interesting discussion with quite a few pros and cons, notably John Chambers ("the father of S and hence R") pretty strongly opposing to use "+" for an operation that is not commutative, and also r-devel archived msg2 (by another R-core member), supporting the view that we (R Core) should not adopt / support the idea; and if people **really* wanted it, they could define %+% for that.

这篇关于为什么不"+"对R中的字符进行操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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