[R] string concatenation operator
Henrik Bengtsson
hb at maths.lth.se
Fri Feb 25 01:47:43 CET 2005
.Primitive() does not necessarily apply non-S3 generic function, cf.
as.character().
An options is to do
"+" <- function(...) UseMethod("+")
"+.default" <- .Primitive("+")
"+.character" <- function(...) paste(...,sep="")
"abc" + "def" + "ghi"
# [1] "abcdefghi"
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.
You might wanna try other binary operators. See "R Language Definition"
under "3.1.4 Operators". For example
"~" <- function(...) UseMethod("~")
"~.default" <- .Primitive("~")
"~.character" <- function(...) paste(...,sep="")
"abc" ~ "def" ~ "ghi"
or
"&" <- function(...) UseMethod("&")
"&.default" <- .Primitive("&")
"&.character" <- function(...) paste(...,sep="")
"abc" & "def" & "ghi"
or
":" <- function(...) UseMethod(":")
":.default" <- .Primitive(":")
":.character" <- function(...) paste(...,sep="")
"abc" : "def" : "ghi"
or
"||" <- function(...) UseMethod("||")
"||.default" <- .Primitive("||")
"||.character" <- function(...) paste(...,sep="")
"abc" || "def" || "ghi"
The "&" or "|" operators are nice but probably not to recommend for similar
reason as "+", but "~" and "||" should be pretty safe considering it is not
common to use multiple times within methods and should not slow things down
too much.
To R experts, except for not being standard procedure, are there reasons why
redefining, say, "~" as above, should be avoided?
Best
Henrik
> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Liaw, Andy
> Sent: Thursday, February 24, 2005 10:18 PM
> To: 'Spencer Graves'
> Cc: r-help at stat.math.ethz.ch
> Subject: RE: [R] string concatenation operator
>
>
> 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
> >
> >
>
> ______________________________________________
> 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
>
>
More information about the R-help
mailing list