"+"支持numeric,integer并Date在r.以下所有操作均有效.
> 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?或者有人可以覆盖+行为以支持其他类型的参数.
你可以重新定义+,但我不推荐它.
`+` <- 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)
| 归档时间: |
|
| 查看次数: |
83 次 |
| 最近记录: |