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

Adrian DUSA adi at roda.ro
Sat Jul 29 12:50:01 CEST 2006


Hi,

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

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



More information about the R-help mailing list