# creating a reverse geometric sequence Classic List Threaded 5 messages Open this post in threaded view
|

## creating a reverse geometric sequence

 Hello, Can anyone think of a non-iterative way to generate a decreasing geometric sequence in R? For example, for a hypothetical function dg, I would like:  > dg(20)  20 10 5 2 1 where I am using integer division by 2 to get each subsequent value in the sequence. There is of course: dg <- function(x) {    res <- integer()    while(x >= 1) {      res <- c(res, x)      x <- x %/% 2    }    res }  > dg(20)  20 10  5  2  1 This implementation of 'dg' uses an interative 'while' loop.  I'm simply wondering if there is a way to vectorize this process? Thanks, Erik ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: creating a reverse geometric sequence

 Erik Iverson wrote: > Hello, > > Can anyone think of a non-iterative way to generate a decreasing geometric > sequence in R? > > For example, for a hypothetical function dg, I would like: > >  > dg(20) >  20 10 5 2 1 > > where I am using integer division by 2 to get each subsequent value in the > sequence. > > > There is of course: > > dg <- function(x) { >    res <- integer() >    while(x >= 1) { >      res <- c(res, x) >      x <- x %/% 2 >    } >    res > } > >  > dg(20) >  20 10  5  2  1 > > This implementation of 'dg' uses an interative 'while' loop.  I'm simply > wondering if there is a way to vectorize this process? >   Something like this should work, at least for integer bases: base <- 2 len <- ceiling(log(x, base)) floor(x/base^(seq_len(len)-1)) Duncan Murdoch ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: creating a reverse geometric sequence

 In reply to this post by Erik Iverson-3 Erik Iverson <[hidden email]> writes: > Hello, > > Can anyone think of a non-iterative way to generate a decreasing > geometric sequence in R? > > For example, for a hypothetical function dg, I would like: > >> dg(20) >  20 10 5 2 1 > > where I am using integer division by 2 to get each subsequent value in > the sequence. > > > There is of course: > > dg <- function(x) { >   res <- integer() >   while(x >= 1) { >     res <- c(res, x) >     x <- x %/% 2 >   } >   res > } > >> dg(20) >  20 10  5  2  1 > > This implementation of 'dg' uses an interative 'while' loop.  I'm > simply wondering if there is a way to vectorize this process? Hi Erik, How about dg <- function(x) {     maxi <- floor(log(x)/log(2))     floor(x / (2^(0:maxi))) } I don't think the remainders cause a problem. Dan > > Thanks, > Erik ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.