[R] array indices in synced vectors

From: Erich Neuwirth <erich.neuwirth_at_univie.ac.at>
Date: Fri 09 Sep 2005 - 00:51:37 EST


Let us start with the following definitions

xxx<-rep(c(1,2),times=5)
yyy<-rep(c(1,2),each=5)
a<-c(11,12)
b<-matrix(1:4,2,2)

a[xxx] produces
[1] 11 12 11 12 11 12 11 12 11 12

b[xxx,yyy] produces

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

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

so it does an implicit "outer" for the indices in xxx and yyy.

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?

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.

-- 
Erich Neuwirth, Didactic Center for Computer Science
University of Vienna
Visit our SunSITE at http://sunsite.univie.ac.at
Phone: +43-1-4277-39902 Fax: +43-1-4277-9399

______________________________________________
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 00:56:47 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:40:05 EST