s-news
[Top] [All Lists]

Re: avoiding a loop in Splus

To: "Leeds, Mark" <mleeds@mlp.com>
Subject: Re: avoiding a loop in Splus
From: Prof Brian Ripley <ripley@stats.ox.ac.uk>
Date: Thu, 28 Oct 2004 12:28:32 +0100 (BST)
Cc: s-news@lists.biostat.wustl.edu
In-reply-to: <54668D97C0199943A454516A5FF6481E58A4F0@EXCHUS001.AD.MLP.COM>
On Thu, 28 Oct 2004, Leeds, Mark wrote:

> does anyone know if it's possible
> to modify the function below to
> avoid the loop ? i think
> i am pretty good at avoiding
> loops in Splus but i can't
> figure this one out.

Please note that _ is deprecated, whereas English is capitalized.

This is a recursive filter, hence filter() could be used.  However, in 
this case there is an explicit solution, something like

outvector <- invector
part <- cumprod(min(invector, 1))
outvector[invector < 1] <- part[invector < 1]


> =================================
>  
> constructLt<-function(invector) {
>  
> outvector_as.vector(length(invector))
> outvector_invector
>  
> for ( i in 2:length(invector) ) {
>  
> if ( invector[i] < 1 )  {
> outvector[i]_invector[i]*outvector[i-1]
> }
> }
>  
> return(outvector)
>  
> } 

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


<Prev in Thread] Current Thread [Next in Thread>