Re: [R] "a"+"b"

From: Matthew Dowle <mdowle_at_concordiafunds.com>
Date: Wed 03 May 2006 - 03:42:24 EST

Perfect. Thank you!

> -----Original Message-----
> From: Martin Morgan [mailto:mtmorgan@fhcrc.org]
> Sent: 02 May 2006 17:10
> To: Matthew Dowle
> Cc: 'Prof Brian Ripley'; "'r-help@/usr/bin/idn:
> idna_to_ascii_4z() failed with error 3.
> <r-help@stat.math.ethz.ch>"@fhcrc.org
> Subject: Re: [R] "a"+"b"
>
>
> I think %any-symbol% defines a binary operator, so
>
> > "%+%" <- function(x,y) paste(x,y,sep="")
> > "a" %+% "b"
> [1] "ab"
> >
>
> or for more fun
>
> > setGeneric("%+%", function(x,y) standardGeneric("%+%"))
> [1] "%+%"
> > setMethod("%+%", signature(x="character", y="character"),
> > function(x,y) paste(x,y,sep=""))
> [1] "%+%"
> > setMethod("%+%", signature(x="numeric", y="numeric"), function(x,y)
> > x+y)
> [1] "%+%"
> > "a" %+% "b"
> [1] "ab"
> > 1 %+% 2
> [1] 3
>
>
> Martin
>
> Matthew Dowle <mdowle@concordiafunds.com> writes:
>
> > Thanks for the information. By 'not easily' it sounds like it is
> > possible, but how? I'm happy to allow 'errors' like
> "2"+"3"="23", is
> > that what an erroneous use could be?
> >
> > I tried the following, given the info in your reply, which
> works. But
> > is there a way to allow "a"+"b"="ab"?
> >> x = "a"
> >> `+.character` <- function(e1, e2) paste(e1,e2, sep="")
> >> attr(x,"class")="character"
> >> x+x
> > [1] "aa"
> >>
> >
> >> -----Original Message-----
> >> From: Prof Brian Ripley [mailto:ripley@stats.ox.ac.uk]
> >> Sent: 02 May 2006 12:03
> >> To: Matthew Dowle
> >> Cc: 'r-help@stat.math.ethz.ch'
> >> Subject: Re: [R] "a"+"b"
> >>
> >>
> >> Not easily. There's a comment in ?Ops that for efficiency
> the group
> >> generics only dispatch on objects with a "class" attribute:
> >> otherwise you
> >> could use an S3 method like
> >>
> >> `+.character` <- function(e1, e2) paste(e1,e2, sep="")
> >>
> >> S4 methods are built on top of S3 methods as far as internal
> >> dispatch is
> >> concerned, so the same comment will apply there AFAICS.
> >>
> >> I would think that the intention was also to positively
> >> discourage messing
> >> with the basics of R, as if you were able to do this
> >> erroneous uses would
> >> likely not get caught.
> >>
> >> On Tue, 2 May 2006, Matthew Dowle wrote:
> >>
> >> >
> >> > Hi,
> >> >
> >> > Is there a way to define "+" so that "a"+"b" returns "ab" ?
> >> >
> >> >> setMethod("+",c("character","character"),paste)
> >> > Error in setMethod("+", c("character", "character"), paste) :
> >> > the method for function '+' and signature e1="character",
> >> > e2="character" is sealed and cannot be re-defined
> >> >> "a"+"b"
> >> > Error in "a" + "b" : non-numeric argument to binary operator
> >> >
> >> > Thanks!
> >> >
> >> >
> >> >
> >> > [[alternative HTML version deleted]]
> >> >
> >> > ______________________________________________
> >> > R-help@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
> >> >
> >>
> >> --
> >> Brian D. Ripley, ripley@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
> >>
> >
> > ______________________________________________
> > R-help@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@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 Received on Wed May 03 03:45:12 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 03 May 2006 - 04:10:27 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.