Hi, if given the value of, say, 15000, I would like to be able to divide that value recursively by, say, 5, and to get a vector of a determined length, say 9, the last value being (set to) zero- i.e. like this: 15000 3000 600 120 24 4.8 0.96 0.192 0 These are in fact concentration values from an experiment. For my script, I get only the starting value (here 15000), and the factor by which concentration is divided for each well, the last one having, by definition, no antagonist at all. I have tried to use "seq", but it can "only" do positive or negative increment. I didn't either find a way with "rep", "sweep" etc. These function normally start from an existing vector, which is not the case here, I have only got a single value to start with. I suppose I could do something "loopy", but I'm sure there is a better way to do it. Thanks a lot for your help, hope the question is not too dumb... Anne-Marie
your.number=15000 your.denominator=5 your.favorite.n=9 your.vector=NULL for(i in 0:your.favorite.n){ your.vector[i+1]=your.number/your.denominator^i your.vector[your.favorite.n+1]=0 } your.vector ##check Best, Daniel ------------------------- cuncta stricte discussurus ------------------------- -----Urspr?ngliche Nachricht----- Von: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im Auftrag von Anne-Marie Ternes Gesendet: Wednesday, July 09, 2008 5:40 AM An: r-help at r-project.org Betreff: [R] recursively divide a value to get a sequence Hi, if given the value of, say, 15000, I would like to be able to divide that value recursively by, say, 5, and to get a vector of a determined length, say 9, the last value being (set to) zero- i.e. like this: 15000 3000 600 120 24 4.8 0.96 0.192 0 These are in fact concentration values from an experiment. For my script, I get only the starting value (here 15000), and the factor by which concentration is divided for each well, the last one having, by definition, no antagonist at all. I have tried to use "seq", but it can "only" do positive or negative increment. I didn't either find a way with "rep", "sweep" etc. These function normally start from an existing vector, which is not the case here, I have only got a single value to start with. I suppose I could do something "loopy", but I'm sure there is a better way to do it. Thanks a lot for your help, hope the question is not too dumb... Anne-Marie ______________________________________________ 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.
On Wed, 2008-07-09 at 11:40 +0200, Anne-Marie Ternes wrote:> Hi, > > if given the value of, say, 15000, I would like to be able to divide > that value recursively by, say, 5, and to get a vector of a determined > length, say 9, the last value being (set to) zero- i.e. like this: > > 15000 3000 600 120 24 4.8 0.96 0.192 0 > > These are in fact concentration values from an experiment. For my > script, I get only the starting value (here 15000), and the factor by > which concentration is divided for each well, the last one having, by > definition, no antagonist at all. > > I have tried to use "seq", but it can "only" do positive or negative > increment. I didn't either find a way with "rep", "sweep" etc. These > function normally start from an existing vector, which is not the case > here, I have only got a single value to start with. > > I suppose I could do something "loopy", but I'm sure there is a better > way to do it. >Well, if you really want to do it recursively (and maybe loopy as well) recursivdiv<-function(x,denom,lendiv,firstpass=TRUE) { if(firstpass) lendiv<-lendiv-1 if(lendiv > 1) { divvec<-c(x/denom,recursivdiv(x/denom,denom,lendiv-1,FALSE)) cat(divvec,ndiv,"\n") } else divvec<-0 if(firstpass) divvec<-c(x,divvec) return(divvec) } Jim
Maybe something like this: f1 with loop, f2 without. f1 <- function(start,len, div) { x <- rep(start,len) for (i in 2 : (len-1)) { x[i] <- x[i-1]/div } x[len] <- 0 return(x) } f2 <- function(start,len, div) { x <- rep(start,len) y <- div^(0:(len-1)) x <- x/y x[length(x)] <- 0 return(x) } system.time(f1(10000,100000,0.5)) system.time(f2(10000,100000,0.5)) Best regards Bart Anne-Marie Ternes wrote:> > Hi, > > if given the value of, say, 15000, I would like to be able to divide > that value recursively by, say, 5, and to get a vector of a determined > length, say 9, the last value being (set to) zero- i.e. like this: > > 15000 3000 600 120 24 4.8 0.96 0.192 0 > > These are in fact concentration values from an experiment. For my > script, I get only the starting value (here 15000), and the factor by > which concentration is divided for each well, the last one having, by > definition, no antagonist at all. > > I have tried to use "seq", but it can "only" do positive or negative > increment. I didn't either find a way with "rep", "sweep" etc. These > function normally start from an existing vector, which is not the case > here, I have only got a single value to start with. > > I suppose I could do something "loopy", but I'm sure there is a better > way to do it. > > Thanks a lot for your help, hope the question is not too dumb... > > Anne-Marie > > ______________________________________________ > 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. > >-- View this message in context: http://www.nabble.com/recursively-divide-a-value-to-get-a-sequence-tp18358046p18358674.html Sent from the R help mailing list archive at Nabble.com.
> a.start <- 15000 > a.step <- 5 > a.length <- 9 > c(a.start*a.step^-(0:(a.length-2)),0)"Anne-Marie Ternes" <amternes at gmail.com> wrote in message news:335d76380807090240o20064c32xb40ca6025bf8d959 at mail.gmail.com...> Hi, > > if given the value of, say, 15000, I would like to be able to divide > that value recursively by, say, 5, and to get a vector of a determined > length, say 9, the last value being (set to) zero- i.e. like this: > > 15000 3000 600 120 24 4.8 0.96 0.192 0 > > These are in fact concentration values from an experiment. For my > script, I get only the starting value (here 15000), and the factor by > which concentration is divided for each well, the last one having, by > definition, no antagonist at all. > > I have tried to use "seq", but it can "only" do positive or negative > increment. I didn't either find a way with "rep", "sweep" etc. These > function normally start from an existing vector, which is not the case > here, I have only got a single value to start with. > > I suppose I could do something "loopy", but I'm sure there is a better > way to do it. > > Thanks a lot for your help, hope the question is not too dumb... > > Anne-Marie > > ______________________________________________ > 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. >
See ?Reduce Reduce("/", rep(5, 9), 15000, acc = TRUE) On Wed, Jul 9, 2008 at 5:40 AM, Anne-Marie Ternes <amternes at gmail.com> wrote:> Hi, > > if given the value of, say, 15000, I would like to be able to divide > that value recursively by, say, 5, and to get a vector of a determined > length, say 9, the last value being (set to) zero- i.e. like this: > > 15000 3000 600 120 24 4.8 0.96 0.192 0 > > These are in fact concentration values from an experiment. For my > script, I get only the starting value (here 15000), and the factor by > which concentration is divided for each well, the last one having, by > definition, no antagonist at all. > > I have tried to use "seq", but it can "only" do positive or negative > increment. I didn't either find a way with "rep", "sweep" etc. These > function normally start from an existing vector, which is not the case > here, I have only got a single value to start with. > > I suppose I could do something "loopy", but I'm sure there is a better > way to do it. > > Thanks a lot for your help, hope the question is not too dumb... > > Anne-Marie > > ______________________________________________ > 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 Anne-Marie, maybe its not particularly elegant, but this function does the trick:> dilute<-function(val,div,len){+ res<-rep(val,len) + res<-res/div^c(0:(len-1)) + res[len]<-0 + res + }> dilute(15000,5,9)Cheers, Ren?> -----Urspr?ngliche Nachricht----- > Von: "Anne-Marie Ternes" <amternes at gmail.com> > Gesendet: 09.07.08 14:12:16 > An: r-help at r-project.org > Betreff: [R] recursively divide a value to get a sequence> Hi, > > if given the value of, say, 15000, I would like to be able to divide > that value recursively by, say, 5, and to get a vector of a determined > length, say 9, the last value being (set to) zero- i.e. like this: > > 15000 3000 600 120 24 4.8 0.96 0.192 0 > > These are in fact concentration values from an experiment. For my > script, I get only the starting value (here 15000), and the factor by > which concentration is divided for each well, the last one having, by > definition, no antagonist at all. > > I have tried to use "seq", but it can "only" do positive or negative > increment. I didn't either find a way with "rep", "sweep" etc. These > function normally start from an existing vector, which is not the case > here, I have only got a single value to start with. > > I suppose I could do something "loopy", but I'm sure there is a better > way to do it. > > Thanks a lot for your help, hope the question is not too dumb... > > Anne-Marie > > ______________________________________________ > 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. >_________________________________________________________________ WEB.DE schenkt Ihnen jeden Monat einen hochkar?tigen Blockbuster von maxdome! Jetzt anmelden unter http://www.blockbuster.web.de