khazaei at ceremade.dauphine.fr
2010-Feb-26 15:23 UTC
[R] question to make a vector without loop
Hello all, I want to define a vector like w[k+1]=w[k]*a/(b+k) for k=1,...,N-1 without use loop. Is it posible to do in R? Regards khazaei
Dear Khazaei, What about this? w <- 1:10 N <- length(w) a <- 1 b <- 1 w[2:(N-1)] <- w[2:(N-1)]*(a/(b+(2:(N-1))))> w[1] 1.0000000 0.6666667 0.7500000 0.8000000 0.8333333 0.8571429 [7] 0.8750000 0.8888889 0.9000000 10.0000000 Note that here I assumed "a" and "b" were constants 1 and 1, respectively. It is no problem to substitute vectors in for them. As long as "a" and "b" are the same length or a multiple of k...N-1, you can substitute them directly in the formula above and R will just keep using them until it is done. Hope that helps, Josh On Fri, Feb 26, 2010 at 7:23 AM, <khazaei@ceremade.dauphine.fr> wrote:> Hello all, > > I want to define a vector like w[k+1]=w[k]*a/(b+k) for k=1,...,N-1 without > use loop. Is it posible to do in R? > > Regards > > khazaei > > ______________________________________________ > 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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Joshua Wiley Senior in Psychology University of California, Riverside http://www.joshuawiley.com/ [[alternative HTML version deleted]]
On 26.02.2010 16:23, khazaei at ceremade.dauphine.fr wrote:> Hello all, > > I want to define a vector like w[k+1]=w[k]*a/(b+k) for k=1,...,N-1 without > use loop. Is it posible to do in R?Sure: wMake <- function(w, a, b, n){ w * (a^(0:(n-1))) / cumprod(c(1, (b+1):(b+n-1))) } wMake(w=1, a=2, b=3, n=10) Uwe Ligges> > Regards > > khazaei > > ______________________________________________ > 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.
My apologies, I misread your formula. Here is a clearer example anyways: w <- 1:10 N <- length(w) a <- 1 b <- 1 k <- 1:(N-1) w[k+1] <- w[k]*(a/(b+k)) w [1] 1.0000000 0.5000000 0.6666667 0.7500000 0.8000000 0.8333333 0.8571429 [8] 0.8750000 0.8888889 0.9000000 Best, Josh On Fri, Feb 26, 2010 at 7:23 AM, <khazaei@ceremade.dauphine.fr> wrote:> Hello all, > > I want to define a vector like w[k+1]=w[k]*a/(b+k) for k=1,...,N-1 without > use loop. Is it posible to do in R? > > Regards > > khazaei > > ______________________________________________ > 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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Joshua Wiley Senior in Psychology University of California, Riverside http://www.joshuawiley.com/ [[alternative HTML version deleted]]
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of > khazaei at ceremade.dauphine.fr > Sent: Friday, February 26, 2010 7:23 AM > To: r-help at r-project.org > Subject: [R] question to make a vector without loop > > Hello all, > > I want to define a vector like w[k+1]=w[k]*a/(b+k) for > k=1,...,N-1 without > use loop. Is it posible to do in R?It would be nice to see your loopy solution. However, you could use cumprod (cumulative products): c(w[1], w[1] * cumprod(a/(b+seq_len(N-1))) E.g., > w<-7 > a<-2 > b<-1 > for(k in 1:9) w[k+1] <- w[k] * a / (b+k) > w [1] 7.0000000000 7.0000000000 4.6666666667 2.3333333333 0.9333333333 [6] 0.3111111111 0.0888888889 0.0222222222 0.0049382716 0.0009876543 > c(w[1], w[1]*cumprod(a/(b+seq_len(9)))) [1] 7.0000000000 7.0000000000 4.6666666667 2.3333333333 0.9333333333 [6] 0.3111111111 0.0888888889 0.0222222222 0.0049382716 0.0009876543 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> > Regards > > khazaei > > ______________________________________________ > 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. >
For general purpose of recursion formula, you could do it like this:> make.vector <- function(w, n, a, b) c(w, sapply(1:(n-1), function(x) w <<- w * a / (b + x))) > make.vector(w = 1, n = 4, a = 24, b = 1)[1] 1 12 96 576 On Fri, Feb 26, 2010 at 11:23 PM, <khazaei at ceremade.dauphine.fr> wrote:> Hello all, > > I want to define a vector like w[k+1]=w[k]*a/(b+k) for k=1,...,N-1 without > use loop. Is it posible to do in R? > > Regards > > khazaei > > ______________________________________________ > 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. >
A general facility for this is Reduce: f <- function(w, k, a = 2, b = 1) w*a / (b+k) c(7, Reduce(f, 2:9, 7, accumulate = TRUE)) the result of which is: c(7, Reduce(f, 2:9, 7, accumulate = TRUE)) [1] 7.0000000000 7.0000000000 4.6666666667 2.3333333333 0.9333333333 0.3111111111 0.0888888889 0.0222222222 0.0049382716 0.0009876543 On Fri, Feb 26, 2010 at 10:23 AM, <khazaei at ceremade.dauphine.fr> wrote:> Hello all, > > I want to define a vector like w[k+1]=w[k]*a/(b+k) for k=1,...,N-1 without > use loop. Is it posible to do in R? > > Regards > > khazaei > > ______________________________________________ > 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. >
Khazaei - I think mapply(function(x,ind)x * a / (b + ind),c(NA,w),c(NA,1:length(w))) does what you want, but since you didn't include a reproducible example, I can't tell for sure. - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Fri, 26 Feb 2010, khazaei at ceremade.dauphine.fr wrote:> Hello all, > > I want to define a vector like w[k+1]=w[k]*a/(b+k) for k=1,...,N-1 without > use loop. Is it posible to do in R? > > Regards > > khazaei > > ______________________________________________ > 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. >