Why doesn't R have one, like "." in Perl or juxtaposition in awk? It does not seem impossible to introduce one, if that would be reasonable. It would seem to involve adding a table entry to main/names.c for the binary operator and a corresponding internal function, say do_dot(). This cannot be simply do_paste(), since the implied separator is "". So, do_dot() might be similar, but if it finds a non-string operand, a syntax error would be issued, in place of the message from do_paste() noting a bad *argument*. Precedence must be decided, but it may not matter. For example, "a" . "b" + 1 If . precedes +, then a syntax error results, after "a" . "b" is computed. If + precedes ., then an error happens, just earlier. What else, or is this a dumb idea?
Harris A. Jaffee <hjaffee <at> jhmi.edu> writes: : Why doesn't R have one, like "." in Perl or juxtaposition in awk? You could define one like this: R> "%+%" <- function(x,y) paste(x,y,sep="") R> "abc" %+% "def" [1] "abcdef"
Not without extending character to another class. If you try to define this "+" method using S4, you get a proper error indicating that addition over characters is not allowed.> setMethod("+", signature("character", "character"),function(e1,e2) paste(e1,e2, sep = "")) Error in setMethod("+", signature("vector", "vector"), function(e1, e2) paste(e1, : The method for function "+" and signature e1="vector", e2="vector" is sealed and cannot be re-defined FYI, your S3 method wouldn't work anyway as the arguments for the + operator are e1, e2 not x, y. -----Original Message----- From: Spencer Graves [mailto:spencer.graves at pdf.com] Sent: Thursday, February 24, 2005 3:46 PM To: Gabor Grothendieck Cc: r-help at stat.math.ethz.ch Subject: Re: [R] string concatenation operator Can we do something to make the following work: > "+.character" <- function(x,y) paste(x,y,sep="") > "abc"+"def" Error in "abc" + "def" : non-numeric argument to binary operator Thanks, spencer graves Gabor Grothendieck wrote:>Harris A. Jaffee <hjaffee <at> jhmi.edu> writes: > >: Why doesn't R have one, like "." in Perl or juxtaposition in awk? > >You could define one like this: > >R> "%+%" <- function(x,y) paste(x,y,sep="") >R> "abc" %+% "def" >[1] "abcdef" > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide!http://www.R-project.org/posting-guide.html> >______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
I believe not, because:> get("+").Primitive("+") i.e., it's not an S3 generic. Andy> From: Spencer Graves > > Can we do something to make the following work: > > > "+.character" <- function(x,y) paste(x,y,sep="") > > "abc"+"def" > Error in "abc" + "def" : non-numeric argument to binary operator > > Thanks, > spencer graves > > Gabor Grothendieck wrote: > > >Harris A. Jaffee <hjaffee <at> jhmi.edu> writes: > > > >: Why doesn't R have one, like "." in Perl or juxtaposition in awk? > > > >You could define one like this: > > > >R> "%+%" <- function(x,y) paste(x,y,sep="") > >R> "abc" %+% "def" > >[1] "abcdef" > > > >______________________________________________ > >R-help at stat.math.ethz.ch mailing list > >https://stat.ethz.ch/mailman/listinfo/r-help > >PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > > > > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >