[R] string concatenation operator
Prof Brian Ripley
ripley at stats.ox.ac.uk
Fri Feb 25 08:48:29 CET 2005
On Fri, 25 Feb 2005, Henrik Bengtsson wrote:
> .Primitive() does not necessarily apply non-S3 generic function, cf.
You mean `imply'? In fact "+" *is* S3 generic and has methods defined:
 +.Date +.POSIXt
It is also group-generic, part of the Ops group with many more methods
via that route. See ?Ops.
> An options is to do
> "+" <- function(...) UseMethod("+")
> "+.default" <- .Primitive("+")
> "+.character" <- function(...) paste(...,sep="")
> "abc" + "def" + "ghi"
> #  "abcdefghi"
[Re-defining R basics is seriously not recommended.]
> The question is if you want to do this. The "+" is probably non-generic for
> a purpose. One may be that having a generic function for such a basic method
> will most likely slow down many methods substantially.
Well, it is generic. The issue is that only coercion between numeric
(broad sense, including complex) types is supported for the arithmetical
operators, presumably to avoid the ambiguity of things like
x <- 123.45
y <- as.character(1)
x + y
Should that be 124.45 or "123.451"? One of the difficulties of any
dispatch on two arguments is how to do the best matching on two classes,
especially with symmetric operators like "+". Internally R favours simple
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help