[R] Equivalent of Mathematica's Apply

From: Juan Pablo Romero Méndez <jpablo.romero_at_gmail.com>
Date: Tue, 24 Jun 2008 14:50:37 -0500


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



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Tue 24 Jun 2008 - 19:54:58 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 Tue 24 Jun 2008 - 21:31:57 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive