[R] Equivalent of Mathematica's Apply

Juan Pablo Romero Méndez jpablo.romero at gmail.com
Tue Jun 24 21:50:37 CEST 2008


Hello,

Is there some equivalent function in R of Mathematica's "Apply"

i.e.

Apply[ f, g[a,b]] ==> f[a,b]


In concret, I have a matrix of indexes:

> ind = cbind(1:5, 1:5+5)

> ind

     [,1] [,2]
[1,]    1    6
[2,]    2    7
[3,]    3    8
[4,]    4    9
[5,]    5   10


and I'd like to extract sequences 1:6, 2:7, 3:8, ..., 5:10  from a vector x.

What would be the most compact way of achieving this?

I've tried

 sapply(1:5, function(i) x[ind[i,1] : ind[i,2]])


Although

 sapply(1:5, function(i) x[seq(ind[i,])])

would be better (if it worked).

Regards,

  Juan Pablo



More information about the R-help mailing list