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.
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.
Hi: ?Recall HTH, Dennis On Mon, Nov 8, 2010 at 1:25 AM, PLucas <plucasplucasplucas@yopmail.com>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]]
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.snow at imail.org
801.408.8111
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of PLucas
> Sent: Monday, November 08, 2010 2:26 AM
> 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-tp3031667p3031667.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.
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.
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.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Greg Snow > Sent: Monday, November 08, 2010 1:15 PM > To: PLucas; r-help at 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.snow at imail.org > 801.408.8111 > > > > -----Original Message----- > > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > > project.org] On Behalf Of PLucas > > Sent: Monday, November 08, 2010 2:26 AM > > 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-tp3031667p3031667.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. > > ______________________________________________ > 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.