[R] nested for loop restriction?

René Capell rene.ca at web.de
Tue Jun 20 14:20:50 CEST 2006


Hello,

is there a restriction for the number of loops in a nested for-loop in R?
I wrote a small function to replace water gage hights by discharge values, where the outer loop walks through the levels of a gage time series and the inner loop looks up the corresponding dicharge value in a vector. 
It seems to work well, with the (however very annoying) restriction that the inner loop stops after 60 steps.
I attached the function at the end of the mail, where tpframe is a 2-column time series dataframe (date,gage hights) and pqframe is a 2-column dataframe (gage hights, discharge).
Any kind of help appreciated,  as I am no native programmer and also quite new to R also replies like "nice try, but there is a more convenient way in R: ...",

thanks in advance,
René


p.zu.q <- function (tpframe, pqframe) {
	tp <- factor(tpframe[[2]])
	tq <- tp
	p <- pqframe[[1]]
	q <- pqframe[[2]]

	for (i in 1:length(levels(tp))) {
		pegel<-levels(tp)[i]

		for (j in 1:length(brugga.p)) {
			if (p[j] == pegel) 
				{levels(tq)[i] <- q[j]
			break
			}
		else next
		}
	next
	}

	tpq <- cbind(tpframe[[1]],as.data.frame(tp),as.data.frame(tq))
	names(tpq) <- c("date","P mm","Q m3/s")
	tpq
} 
_____________________________________________________________________
Der WEB.DE SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!



More information about the R-help mailing list