[R] recursively divide a value to get a sequence

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Wed Jul 9 16:01:05 CEST 2008


two alternative solutions with a more functional-style taste:

dilute = function(init, div, count)
    sapply(1:count, function(i) init/div^(i-1))

dilute = function(init, div, count)
    if (count == 1) init
    else c(init, dilute(init/div, div, count-1))

vQ



René Capell wrote:
> 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
>
> ______________________________________________
> 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.
>   


-- 
-------------------------------------------------------------------------------
Wacek Kusnierczyk, MD PhD

Email: waku at idi.ntnu.no
Phone: +47 73591875, +47 72574609

Department of Computer and Information Science (IDI)
Faculty of Information Technology, Mathematics and Electrical Engineering (IME)
Norwegian University of Science and Technology (NTNU)
Sem Saelands vei 7, 7491 Trondheim, Norway
Room itv303

Bioinformatics & Gene Regulation Group
Department of Cancer Research and Molecular Medicine (IKM)
Faculty of Medicine (DMF)
Norwegian University of Science and Technology (NTNU)
Laboratory Center, Erling Skjalgsons gt. 1, 7030 Trondheim, Norway
Room 231.05.060



More information about the R-help mailing list