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'
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/