[R] assignment function
Christian Hoffmann
christian.hoffmann at wsl.ch
Wed Feb 14 11:09:36 CET 2001
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list