Re: [R] any alternatives for complex for-loops?

From: Kinoko <>
Date: Tue, 11 Nov 2008 21:48:11 -0800 (PST)

Thanks for the replies

Sorry for being unclear.

I am asking if there is a way to process a vector in a way that uses references to other elements of the same vector. And doing this without a for-loop.

Here is a running code:


complexFn <- function(a,b){
  c <- (a+b)/2

x <- 1:10
y <- rep(NA, length(x))

for (i in 1:length(x)){


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

And here is another attempt without the for-loop.


x <- 1:10
x1 <- c(NA, x)


If someone could tell me the normal/elegant/effective/R way of doing this kind of vector processing, that would highly appreciated.


gabor mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Wed 12 Nov 2008 - 05:52:56 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 12 Nov 2008 - 09:30:25 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive