'+'运算符支持r中的非数字参数

MKR*_*MKR -1 r

"+"支持numeric,integerDater.以下所有操作均有效.

> 5 + 5L
[1] 10
> Sys.Date() + 5
[1] "2018-01-18"
Run Code Online (Sandbox Code Playgroud)

+两者之间不起作用character.喜欢:

> "one" + "one"
Error in "one" + "one" : non-numeric argument to binary operator
> "What is date today? Ans:" + Sys.Date()
Error in unclass(e1) + unclass(e2) : 
  non-numeric argument to binary operator
Run Code Online (Sandbox Code Playgroud)

除了numeric参数之外是否禁止允许r?或者有人可以覆盖+行为以支持其他类型的参数.

ala*_*han 7

你可以重新定义+,但我不推荐它.

`+` <- function(x, y) UseMethod("+")
`+.character` <- function(x, y) paste0(x, y)
`+.default` <- .Primitive("+")

1 + 1 ##2
"a" + "b" ##"ab"
"a" + 2 ##"a2"
Run Code Online (Sandbox Code Playgroud)