Re: [R] array indices in synced vectors

From: Thomas Lumley <tlumley_at_u.washington.edu>
Date: Fri 09 Sep 2005 - 00:58:39 EST

On Thu, 8 Sep 2005, Erich Neuwirth wrote:
>
> sapply(1:length(xxx),function(x)b[xxx[x],yyy[x]])
> does what I need and produces
> [1] 1 2 1 2 1 4 3 4 3 4
>
> Is there a function taking xxx,yyy, and b as arguments
> producing the same result?

b[cbind(xxx,yyy)]

> Essentially, I am asking for a version of lapply and/or sapply
> which works with functions of more than one argument and takes the
> iteration arguments as vectors or lists of equal length.

More generally there is mapply(), but the matrix subscript solution is better in this example
> mapply(function(i,j) b[i,j], xxx,yyy)
  [1] 1 2 1 2 1 4 3 4 3 4

         -thomas



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri Sep 09 01:06:43 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 16:36:51 EST