[R] assignment function

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Feb 14 11:31:05 CET 2001


The last argument of any assignment function has to be named `value'.
This is different from S, and is in the FAQ, section 3.3.3.


> Date: Wed, 14 Feb 2001 11:09:36 +0100
> To: R Help list <r-help at stat.math.ethz.ch>
> From: Christian Hoffmann <christian.hoffmann at wsl.ch>
> Subject: [R] assignment function
> 
> Hi,
> 
> I am trying to create the assignment function:
> "substring<-" <- function(text, first, last=100000, sub) {
>   if(is.character(first)) {
>    if(!missing(last)) stop('wrong # arguments')
>    return(sedit(text, first, sub))  
>  }
> 
>   lf <- length(first)
> 
>   if(length(text)==1 && lf > 1) {
>   	if(missing(last)) last <- nchar(text)
>   	last <- rep(last, length=lf)
>   	for(i in 1:lf) {
>   	  text <- paste(if(first[i]>1) 
>               substring(text, 1, first[i]-1), sub,
>         	    substring(text, last[i]+1), sep='')
> 	    if(i < lf) {
> 	      j <- (i+1):lf
> 	      w <- nchar(sub) - (last[i]-first[i]+1)
> 	      first[j] <- first[j] + w  
> 	      last[j] <- last[j] +  w
> 	    }
>    	}
>   	return(text)
>   }
>   res <- paste(ifelse(first>1,substring(text, 1, first-1),''), sub,
>               substring(text, last+1), sep='')
>   res
> }
> 
> Checking
> > x <- 'this string'
> > substring(x, 3, 4) <- 'IS'
> 
> will not result in
> > x [1] "thIS string"
> 
> but will cry
> Error in substr<-(*tmp*, 3, 4, value = "IS") : 
> 	unused argument(s) (value ...)
> 
> A cat(is.character(first),"\n") after the first line will not even be
> executed.
> 
> 
> Thanks for help.
> --christian
> 
> Dr.sc.math.Christian W. Hoffmann
> Mathematics and Statistical Computing
> Landscape Modeling and Web Applications
> Swiss Federal Research Institute WSL 
> Zuercherstrasse 111
> CH-8903 Birmensdorf, Switzerland
> phone: ++41-1-739 22 77    fax: ++41-1-739 22 15
> e-mail: Hoffmann at WSL.CH
> www: http://www.wsl.ch/staff/christian.hoffmann/
> 
> 
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> 
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

-- 
Brian D. Ripley,                  ripley at 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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list