[R] any alternatives for complex for-loops?

Ray Brownrigg Ray.Brownrigg at mcs.vuw.ac.nz
Wed Nov 12 05:05:08 CET 2008


Well, there is an implicit for loop, expressed by:

x[i] = complexFn(x[i-1], x[i-2])

But my response is along the lines of:

It really depends what your "..." is.

How about something like (pseudo-code again):

x[1] <- something
x[2] <- something_else

x = complexFn(x)

so the vectorisation is taken care of inside complexFn.

Ray Brownrigg

On Wed, 12 Nov 2008, hadley wickham wrote:
> On Tue, Nov 11, 2008 at 8:30 PM, Kinoko <andzsinszan at gmail.com> wrote:
> > Dear list,
> >
> > Is there a way to do something like the following pseudo-code -
> > without for loop?
>
> There isn't a for loop in your code!
>
> Hadley
>
> > complexFn <- function(a,b){
> >  ...
> >  return(c)
> > }
> >
> > x[i] = complexFn(x[i-1], x[i-2])
> >
> >
> > I have found "filter" but would like to do something more complicated
> > than multiplication with x[i-1] and x[i-2].
> >
> > Thanks in advance!
> >
> > gabor
> >



More information about the R-help mailing list