[R] Nested functions
John Wiedenhoeft
wiedenhoeft at gmx.net
Tue Jul 18 17:35:16 CEST 2006
Am Dienstag, den 18.07.2006, 22:09 -0400 schrieb Jim Lemon:
> Hi John,
>
> Minor bug - I zeroed the hit counter in the wrong place.
>
> find.replay<-function(tunestring,maxlen) {
....
> return(matchlist)
> }
Dear Jim,
many, many thanks for your effords :-D!!! Your program is great and very
elegant (guess I was thinking to mathematical and iterative...). I have
made some minor adjustments to it. Most important, I have changed
starttest to startpat+1, since patterns can overlap of course (1111
consists of 2 times 111). Hope this doesn't cause any inconsistency.
Here is what I'm using now:
CODECODECODECODECODECODECODECODECODECODECODECODECODECODECODE
find.replay<-function(tunestring,filename,maxlen)
{
tunelen<-length(tunestring)
if(missing(maxlen)) maxlen<-floor(tunelen/2)
if(missing(filename)) filename<-"output"
matchlist <- list()
startpat<-1
endpat<-2
finishpos<-tunelen-maxlen
pattern<-tunestring[startpat:endpat]
patlen<-length(pattern)
while(patlen <= maxlen)
{
while(endpat < tunelen-patlen)
{
starttest<-startpat+1 #changed from endpat+1 to detect overlapping
patterns
endtest<-starttest+(endpat-startpat)
# step through the rest of tunestring with this pattern
while(endtest <= tunelen)
{
testpat<-tunestring[starttest:endtest]
if(identical(pattern,testpat))
{
m <- 0;
w <- 0;
for(k in 1:patlen)
{m <- pattern[k]*10^(patlen-k)+m;}
for(l in 1:patlen)
{w <- testpat[l]*10^(patlen-l)+w;}
#just in case... ;-)
if (m!=w)
{
warn <- paste("Unmatching patterns", m, "and", w, "detected
errorneously!")
print(warn)
write.table(warn, file=filename, append=TRUE, sep="\t", eol="\n",
row.names=FALSE, col.names=FALSE);
}
p <- c(patlen, m, startpat, starttest);
write.table(t(p), file=filename, append=TRUE, sep="\t", eol="\n",
row.names=FALSE, col.names=FALSE);
# print(p);
}
# step to the next substring
starttest<-starttest+1
endtest<-endtest+1
}
# now move pattern one step along the string
startpat<-startpat+1
endpat<-endpat+1
pattern<-tunestring[startpat:endpat]
}
# now increase the length of the pattern
startpat<- 1
endpat<-startpat+patlen
pattern<-tunestring[startpat:endpat]
patlen<-length(pattern)
}
print(paste("Output written to file '", filename, "'. Have fun!",
sep=""))
}
ENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDEND
Again, many, many thanks. You brightened up my day!
Cheers,
John
More information about the R-help
mailing list