Re: [R] Nested functions

From: John Wiedenhoeft <wiedenhoeft_at_gmx.net>
Date: Wed 19 Jul 2006 - 01:35:16 EST

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



R-help@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 and provide commented, minimal, self-contained, reproducible code. Received on Wed Jul 19 01:40:37 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 19 Jul 2006 - 02:17:01 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.