Re: [R] How to eliminate this for loop ?
OK, double oops. I first tested my code with length 100, then upped the number but forgot to up the preallocation part, I should have used a variable there instead so that only one place needed to be changed. My version did have problems when I tried to do a vector of length 10,000, some values were NaN probably due to b^largenumber being essentially 0, then being in the denominator. Though for really long vectors the round off error in any version could accumulate to the point of affecting the results. This could start a debate about whether the missing value could be seen as better than a potentially incorrect non-missing value. It would mainly depend on the purpose and I don't think there would be a general preference either way. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.s...@imail.org 801.408.8111 > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- > project.org] On Behalf Of William Dunlap > Sent: Tuesday, November 09, 2010 10:07 AM > To: PLucas; r-help@r-project.org > Subject: Re: [R] How to eliminate this for loop ? > > Note that for long vectors the OP's code would > go much faster if he preallocated the output vector > a to its eventual length. I.e., start with >a <- numeric(N) > instead of >a <- c() > I defined 2 functions that differed only in how > a was initialized >f0 <- function(b, c) { > N <- length(c) > 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 > } > a >} > >f1 <- function(b, c) { > N <- length(c) > a<- numeric(N) > 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 > } > a >} > and timed them for a 100,000 long vector: >> c <- rnorm(1e5) >> system.time(a0 <- f0(b=0.5, c=c)) > user system elapsed > 17.270 1.410 18.704 >> system.time(a1 <- f1(b=0.5, c=c)) > user system elapsed > 0.400 0.000 0.401 >> identical(a0, a1) >[1] TRUE > If that is not fast enough then you have to > start thinking harder. E.g., look at functions > like filter() and/or do some algebra. > > (Greg's code also had an error of that sort, > preallocating 100 entries where the eventual > length was 1000). > > Bill Dunlap > Spotfire, TIBCO Software > wdunlap tibco.com > > > -----Original Message----- > > From: r-help-boun...@r-project.org > > [mailto:r-help-boun...@r-project.org] On Behalf Of Greg Snow > > Sent: Tuesday, November 09, 2010 8:31 AM > > To: Greg Snow; PLucas; r-help@r-project.org > > Subject: Re: [R] How to eliminate this for loop ? > > > > Oops, my version added cc instead of subtracted, it still > > works if you multiply cc by -1 (except the initial 1). > > > > -- > > Gregory (Greg) L. Snow Ph.D. > > Statistical Data Center > > Intermountain Healthcare > > greg.s...@imail.org > > 801.408.8111 > > > > > > > -Original Message- > > > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- > > > project.org] On Behalf Of Greg Snow > > > Sent: Monday, November 08, 2010 1:15 PM > > > To: PLucas; r-help@r-project.org > > > Subject: Re: [R] How to eliminate this for loop ? > > > > > > If you are willing to shift the c vector by 1 and have 1 > > (the initial > > > value) as the start of c, then you can just do: > > > > > > cumsum( cc * b^( (n-1):0 ) ) / b^( (n-1):0 ) > > > > > > to compare: > > > > > > cc <- c(1, rnorm(999) ) > > > b <- 0.5 > > > n <- length(cc) > > > > > > a1 <- numeric(100) > > > a1[1] <- 1 > > > > > > system.time(for(i in 2:n ) { > > > a1[i] <- b*a1[i-1] + cc[i] > > > }) > > > > > > system.time(a2 <- cumsum( cc * b^( (n-1):0 ) ) / b^( (n-1):0 )) > > > > > > all.equal(a1,a2) > > > > > > Though you could have problems with the b^ part if the > > length gets too > > > long. > > > > > > -- > > > Gregory (Greg) L. Snow Ph.D. > > > Statistical Data Center > > > Intermountain Healthcare > > > greg.s...@imail.org > > > 801.408.8111 > > > > > > > > > > -Original Message- > > > > From: r-help-boun...@r-proj
Re: [R] How to eliminate this for loop ?
Note that for long vectors the OP's code would go much faster if he preallocated the output vector a to its eventual length. I.e., start with a <- numeric(N) instead of a <- c() I defined 2 functions that differed only in how a was initialized f0 <- function(b, c) { N <- length(c) 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 } a } f1 <- function(b, c) { N <- length(c) a<- numeric(N) 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 } a } and timed them for a 100,000 long vector: > c <- rnorm(1e5) > system.time(a0 <- f0(b=0.5, c=c)) user system elapsed 17.270 1.410 18.704 > system.time(a1 <- f1(b=0.5, c=c)) user system elapsed 0.400 0.000 0.401 > identical(a0, a1) [1] TRUE If that is not fast enough then you have to start thinking harder. E.g., look at functions like filter() and/or do some algebra. (Greg's code also had an error of that sort, preallocating 100 entries where the eventual length was 1000). Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -Original Message- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of Greg Snow > Sent: Tuesday, November 09, 2010 8:31 AM > To: Greg Snow; PLucas; r-help@r-project.org > Subject: Re: [R] How to eliminate this for loop ? > > Oops, my version added cc instead of subtracted, it still > works if you multiply cc by -1 (except the initial 1). > > -- > Gregory (Greg) L. Snow Ph.D. > Statistical Data Center > Intermountain Healthcare > greg.s...@imail.org > 801.408.8111 > > > > -Original Message- > > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- > > project.org] On Behalf Of Greg Snow > > Sent: Monday, November 08, 2010 1:15 PM > > To: PLucas; r-help@r-project.org > > Subject: Re: [R] How to eliminate this for loop ? > > > > If you are willing to shift the c vector by 1 and have 1 > (the initial > > value) as the start of c, then you can just do: > > > > cumsum( cc * b^( (n-1):0 ) ) / b^( (n-1):0 ) > > > > to compare: > > > > cc <- c(1, rnorm(999) ) > > b <- 0.5 > > n <- length(cc) > > > > a1 <- numeric(100) > > a1[1] <- 1 > > > > system.time(for(i in 2:n ) { > > a1[i] <- b*a1[i-1] + cc[i] > > }) > > > > system.time(a2 <- cumsum( cc * b^( (n-1):0 ) ) / b^( (n-1):0 )) > > > > all.equal(a1,a2) > > > > Though you could have problems with the b^ part if the > length gets too > > long. > > > > -- > > Gregory (Greg) L. Snow Ph.D. > > Statistical Data Center > > Intermountain Healthcare > > greg.s...@imail.org > > 801.408.8111 > > > > > > > -Original Message- > > > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- > > > project.org] On Behalf Of PLucas > > > Sent: Monday, November 08, 2010 2:26 AM > > > To: r-help@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-tp3031667p3031667.html > > > Sent from the R help mailing list archive at Nabble.com. > > > > > > __ > > > R-help@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. > > > > __ > > R-help@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. > > __ > R-help@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. > __ R-help@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.
Re: [R] How to eliminate this for loop ?
Erich: (Assuming this is correct), this is very nice. However, I just wanted to point out that if you look at the code for Reduce, you'll find it's implemented with for loops. So the OP's original version using a for loop is likely to be faster (just as it's likely to be faster than my actual recursive version). Cheers, Bert On Mon, Nov 8, 2010 at 4:14 PM, Erich Neuwirth wrote: > Reduce(function(x1,x2)b*x1-x2,c,init=1,accum=TRUE) > > might be what you are looking for. > This is not fully tested, so you should test it before > you want to use it. > > __ > R-help@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. > -- Bert Gunter Genentech Nonclinical Biostatistics __ R-help@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.
Re: [R] How to eliminate this for loop ?
Oops, my version added cc instead of subtracted, it still works if you multiply cc by -1 (except the initial 1). -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.s...@imail.org 801.408.8111 > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- > project.org] On Behalf Of Greg Snow > Sent: Monday, November 08, 2010 1:15 PM > To: PLucas; r-help@r-project.org > Subject: Re: [R] How to eliminate this for loop ? > > If you are willing to shift the c vector by 1 and have 1 (the initial > value) as the start of c, then you can just do: > > cumsum( cc * b^( (n-1):0 ) ) / b^( (n-1):0 ) > > to compare: > > cc <- c(1, rnorm(999) ) > b <- 0.5 > n <- length(cc) > > a1 <- numeric(100) > a1[1] <- 1 > > system.time(for(i in 2:n ) { > a1[i] <- b*a1[i-1] + cc[i] > }) > > system.time(a2 <- cumsum( cc * b^( (n-1):0 ) ) / b^( (n-1):0 )) > > all.equal(a1,a2) > > Though you could have problems with the b^ part if the length gets too > long. > > -- > Gregory (Greg) L. Snow Ph.D. > Statistical Data Center > Intermountain Healthcare > greg.s...@imail.org > 801.408.8111 > > > > -Original Message- > > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- > > project.org] On Behalf Of PLucas > > Sent: Monday, November 08, 2010 2:26 AM > > To: r-help@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-tp3031667p3031667.html > > Sent from the R help mailing list archive at Nabble.com. > > > > __ > > R-help@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. > > __ > R-help@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. __ R-help@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.
Re: [R] How to eliminate this for loop ?
I doubt this to be true. Try this in R: > dmy<-rep(1,5) > dmy[2:5]<-dmy[1:4]+1 This is equivalent to what you propose (even simpler), but it does not, as OP seems to have wanted, fill dmy with 1,2,3,4,5, but, as I had expected, with 1,2,2,2,2. I would be interested in knowing what exactly the difference beween my example above, and the one you suggest, is. As others have suggested: another way is to use actual recursive calls, but I seriously doubt these to be more efficient. You should probably only use it if you really hate to type the word 'for' (-: Though I would also like to see an example where they prove to be the better way to go (by any criteria, but preferably speed or perhaps other resource usage) Nick Sabbe -- ping: nick.sa...@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: David Winsemius [mailto:dwinsem...@comcast.net] Sent: maandag 8 november 2010 15:04 To: Nick Sabbe Cc: 'PLucas'; r-help@r-project.org Subject: Re: [R] How to eliminate this for loop ? On Nov 8, 2010, at 4:30 AM, Nick Sabbe wrote: > Whenever you use a recursion (that cannot be expressed otherwise), you > always need a (for) loop. Not necessarily true ... assuming "a" is of length "n": a[2:n] <- a[1:(n-1))]*b + cc[1:(n-1)] # might work if b and n were numeric vectors of length 1 and cc had length >= n. (Never use "c" as a vector name.) # it won't work if there are no values for the nth element at the beginning and you are building up a element by element. And you always need to use operations that appropriate to the object type. So if "a" really is a list, this will always fail since arithmetic does not work on list elements. If on the other hand, the OP were incorrect in calling this a list and "a" were a numeric vector, there might be a chance of success if the rules of indexing were adhered to. The devil is in the details and the OP has not supplied enough code to tell what might happen. -- David. > 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.sa...@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-boun...@r-project.org [mailto:r-help-boun...@r-project.org > ] On > Behalf Of PLucas > Sent: maandag 8 november 2010 10:26 > To: r-help@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@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. > > __ > R-help@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. David Winsemius, MD West Hartford, CT __ R-help@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.
Re: [R] How to eliminate this for loop ?
Reduce(function(x1,x2)b*x1-x2,c,init=1,accum=TRUE) might be what you are looking for. This is not fully tested, so you should test it before you want to use it. __ R-help@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.
Re: [R] How to eliminate this for loop ?
If you are willing to shift the c vector by 1 and have 1 (the initial value) as the start of c, then you can just do: cumsum( cc * b^( (n-1):0 ) ) / b^( (n-1):0 ) to compare: cc <- c(1, rnorm(999) ) b <- 0.5 n <- length(cc) a1 <- numeric(100) a1[1] <- 1 system.time(for(i in 2:n ) { a1[i] <- b*a1[i-1] + cc[i] }) system.time(a2 <- cumsum( cc * b^( (n-1):0 ) ) / b^( (n-1):0 )) all.equal(a1,a2) Though you could have problems with the b^ part if the length gets too long. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.s...@imail.org 801.408.8111 > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- > project.org] On Behalf Of PLucas > Sent: Monday, November 08, 2010 2:26 AM > To: r-help@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-tp3031667p3031667.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@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. __ R-help@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.
Re: [R] How to eliminate this for loop ?
1. The for loop is probably as or more efficient than recursion, especially for large n (comments/corrections on this claim from cogniscenti are welcome); 2. One can **Always** express a for loop recursively -- that is the nature of computer languages (comments/corrections again welcome). 3. Here is code that does it: Note that I have changed argument name "c" to "const" to avoid confusion with the c() function f <- function(n,b,const) { if(length(const)< n-1)stop("const is too short") g <- function(i){ if(i==1) 1 else b*g(i-1)+ const[i-1] } g(n) } As David intimated, this produces a vector, not a list. Cast with as.list if that's what you want. Cheers, Bert On Mon, Nov 8, 2010 at 6:03 AM, David Winsemius wrote: > > On Nov 8, 2010, at 4:30 AM, Nick Sabbe wrote: > >> Whenever you use a recursion (that cannot be expressed otherwise), you >> always need a (for) loop. > > Not necessarily true ... assuming "a" is of length "n": > > a[2:n] <- a[1:(n-1))]*b + cc[1:(n-1)] > # might work if b and n were numeric vectors of length 1 and cc had length >>= n. (Never use "c" as a vector name.) > # it won't work if there are no values for the nth element at the beginning > and you are building up a element by element. > > And you always need to use operations that appropriate to the object type. > So if "a" really is a list, this will always fail since arithmetic does not > work on list elements. If on the other hand, the OP were incorrect in > calling this a list and "a" were a numeric vector, there might be a chance > of success if the rules of indexing were adhered to. The devil is in the > details and the OP has not supplied enough code to tell what might happen. > > -- > David. > >> 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.sa...@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-boun...@r-project.org [mailto:r-help-boun...@r-project.org] >> On >> Behalf Of PLucas >> Sent: maandag 8 november 2010 10:26 >> To: r-help@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@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. >> >> __ >> R-help@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. > > David Winsemius, MD > West Hartford, CT > > __ > R-help@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. > -- Bert Gunter Genentech Nonclinical Biostatistics 467-7374 http://devo.gene.com/groups/devo/depts/ncb/home.shtml __ R-help@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.
Re: [R] How to eliminate this for loop ?
On Nov 8, 2010, at 4:30 AM, Nick Sabbe wrote: Whenever you use a recursion (that cannot be expressed otherwise), you always need a (for) loop. Not necessarily true ... assuming "a" is of length "n": a[2:n] <- a[1:(n-1))]*b + cc[1:(n-1)] # might work if b and n were numeric vectors of length 1 and cc had length >= n. (Never use "c" as a vector name.) # it won't work if there are no values for the nth element at the beginning and you are building up a element by element. And you always need to use operations that appropriate to the object type. So if "a" really is a list, this will always fail since arithmetic does not work on list elements. If on the other hand, the OP were incorrect in calling this a list and "a" were a numeric vector, there might be a chance of success if the rules of indexing were adhered to. The devil is in the details and the OP has not supplied enough code to tell what might happen. -- David. 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.sa...@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-boun...@r-project.org [mailto:r-help-boun...@r-project.org ] On Behalf Of PLucas Sent: maandag 8 november 2010 10:26 To: r-help@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@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. __ R-help@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. David Winsemius, MD West Hartford, CT __ R-help@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.
Re: [R] How to eliminate this for loop ?
Hi: ?Recall HTH, Dennis On Mon, Nov 8, 2010 at 1:25 AM, PLucas wrote: > > 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-tp3031667p3031667.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@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. > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] How to eliminate this for loop ?
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.sa...@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-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of PLucas Sent: maandag 8 november 2010 10:26 To: r-help@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@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. __ R-help@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.
[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-tp3031667p3031667.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.