0

The "+" supports numeric, integer and Date in r. All below operations are valid.

> 5 + 5L
[1] 10
> Sys.Date() + 5
[1] "2018-01-18"

But + doesn't work between character. Like:

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

Is it forbidden to allow other than numeric argument in r? Or someone can overwrite + behavior to support other types of argument.

MKR
  • 19,150
  • 4
  • 21
  • 32

1 Answers1

7

You could redefine +, though I don't recommend it.

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

1 + 1 ##2
"a" + "b" ##"ab"
"a" + 2 ##"a2"
alan ocallaghan
  • 3,044
  • 15
  • 35