Re: [R] Extracting from a matrix w/o for-loop

From: Adrian DUSA <>
Date: Sat 29 Jul 2006 - 20:50:01 EST


On Friday 28 July 2006 20:21, Horace Tso wrote:
> Unless there is another level of complexity that i didn't see here,
> wouldn't it be a simply application of sapply as follow,
> sapply( 1:dim(M2)[[1]], function(x) M1[M2[x,1], M2[x,2]] )

Andy's previous answer involving matrix indexing (M1[M2]) is simpler but just for the sake of it, since we're dealing with matrices it is not a case of sapply but of _apply_:

apply(M2, 1, function(x) M1[x[1], x[2]])

My 2c,

Adrian DUSA
Arhiva Romana de Date Sociale
Bd. Schitu Magureanu nr.1
050025 Bucuresti sectorul 5
Tel./Fax: +40 21 3126618 \
          +40 21 3120210 / int.101

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Sat Jul 29 20:52:52 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sun 30 Jul 2006 - 00:16:37 EST.

Mailing list information is available at Please read the posting guide before posting to the list.