[R] A technical question on methods for "+"

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Thu Dec 2 20:31:47 CET 2021

... and probably a dumb one and almost certainly not of interest to
most R users. But anyway...

?"+" says:
"The unary and binary arithmetic operators are generic functions:
methods can be written for them individually or via the Ops group
generic function. "

"+.character" <- function(e1, e2) paste0(e1, e2)
## but this doesn't 'work':
> "a" + "b"
Error in "a" + "b" : non-numeric argument to binary operator

## but explicitly invoking the method does 'work' :
> "+.character"('a','b')
[1] "ab"

##Note also:
> methods("+")
[1] +.character +.Date      +.IDate*    +.POSIXt    +.trellis*

So what am I failing to understand?

Bert Gunter

More information about the R-help mailing list