22

Call me lazy, but I just hate typing things like paste("a","b",sep='') all the time.

I know that "(t)his is R. There is no if, only how." (library(fortunes);(fortune(109)). So, my follow up question is: Is it possible to easily change this behavior?

jogo
  • 12,469
  • 11
  • 37
  • 42
Eduardo Leoni
  • 8,991
  • 6
  • 42
  • 49
  • The problem is how to define '+' on characters. You want sep = "" but others might prefer the default sep = " ". And what if one of both element is not a character (numeric, logical). How should '+' then be defined? – Thierry Aug 24 '09 at 08:17
  • "+" would be a binary operator, so the 'sep' argument is not relevant. There would be choices to make ("character"+numeric, etc) but I (and I suspect most people) would be fine with any decision, including just allowing characters to be concatenated. That's what python does, for example. The discussion linked by Martin below is really interesting. – Eduardo Leoni Aug 24 '09 at 15:04

2 Answers2

26

@ Dirk: For once, you're not quite right. It's not the parser. One can write methods in R for "+" -- help("+") goes to "Arithmetic operators" and mentions that these are generic and you can write methods for them ... and of course many package writers do, e.g., we do for the 'Matrix' package, and I also do for the "Rmpfr" package, e.g. But Dirk is also right (of course!) that you cannot do it in R currently, by just defining a method for "+.character".

About three years ago, I had started a thread on R-devel (the R mailing list on R development; very much recommended if you are interested in these topics; you can also access through Gmane if you don't want to subscribe) :r-devel archived msg

It came to an interesting discussion with quite a few pros and cons, notably John Chambers ("the father of S and hence R") pretty strongly opposing to use "+" for an operation that is not commutative, and also r-devel archived msg2 (by another R-core member), supporting the view that we (R Core) should not adopt / support the idea; and if people **really* wanted it, they could define %+% for that.

Joris Meys
  • 106,551
  • 31
  • 221
  • 263
Martin Mächler
  • 4,619
  • 27
  • 27
  • 2
    I noticed Hadley wrote some methods for "+" in his ggplot2 package. There is is used to add multiple layers on a plot. – Thierry Aug 24 '09 at 11:00
  • 2
    Excellent thread you started there, thanks! There seems to be a conflict between those that don't believe that it is natural to think of "+" as concatenation (e.g. Chambers) and those that see it all the time in other languages and are used to it. Has the discussion simply stopped there? – Eduardo Leoni Aug 24 '09 at 14:59
6

Is using sprintf any more convenient for you?

Barring that, how about this little sleight of hand:

'%+%' <- paste

'and' %+% 'now' %+% 'for'%+% 'something' %+% 'completely' %+% 'different'    
# [1] "and now for something completely different"
Scarabee
  • 5,437
  • 5
  • 29
  • 55
Steve Lianoglou
  • 7,183
  • 2
  • 25
  • 21
  • 1
    sprintf is no more convenient in the default case (no formatting) but I should use it more often. Thanks for the suggestion of using "%+%", though this actually does not answer the main question. – Eduardo Leoni Aug 23 '09 at 23:38
  • 2
    There is no answer -- you cannot override + as an operator due to the way the parser works. Steve's suggestions is your best bet, apart from maybe defining a shortcut function p <- function(...) paste(..., sep="") which may be easier to type than the usual paste() with sep="". – Dirk Eddelbuettel Aug 24 '09 at 02:01
  • 1
    Okay, so we finally found a counter example to fortune(109). More accurately, the answer is "write your own damn parser" :) – Eduardo Leoni Aug 24 '09 at 03:09