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
|