I use mapply
for that. For example
a<-1:1000
mapply(function(x,y)x-y,a[-1000],a[-1])
It appears to be slightly faster than the for loop version:
> f <- function(x,y)x-y
> g <- function(){
o<-c();
for(i in a[-1000])o<-c(o,f(i,i+1))
> }
>
> system.time(
+ for(i in 1:1000){
+ mapply(f,a[-1000],a[-1])
+ }
+ )
user system elapsed
2.344 0.000 2.345
> system.time(for(i in 1:1000)g())
user system elapsed
3.399 0.000 3.425