[R] string concatenation operator

McGehee, Robert Robert.McGehee at geodecapital.com
Thu Feb 24 22:17:14 CET 2005


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




More information about the R-help mailing list