[R] How to eliminate this for loop ?

Nick Sabbe nick.sabbe at ugent.be
Mon Nov 8 10:30:59 CET 2010


Whenever you use a recursion (that cannot be expressed otherwise), you
always need a (for) loop.
Apply and the like do not allow to use the intermediary results (i.e. a[i-1]
to calculate a[i]).

So: no, it cannot be avoided in your case, I guess.


Nick Sabbe
--
ping: nick.sabbe at ugent.be
link: http://biomath.ugent.be
wink: A1.056, Coupure Links 653, 9000 Gent
ring: 09/264.59.36

-- Do Not Disapprove



-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of PLucas
Sent: maandag 8 november 2010 10:26
To: r-help at r-project.org
Subject: [R] How to eliminate this for loop ?


Hi, I would like to create a list recursively and eliminate my for loop :

a<-c()
a[1] <- 1; # initial value
for(i in 2:N) {
	a[i]<-a[i-1]*b - c[i-1] # b is a value, c is another vector
}


Is it possible ?

Thanks
-- 
View this message in context:
http://r.789695.n4.nabble.com/How-to-eliminate-this-for-loop-tp3031667p30316
67.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help at r-project.org 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.



More information about the R-help mailing list