[R] How to index a matrix with different row-number for each column?

Zhenjiang Lan lan.zhenjiang at gmail.com
Tue Jun 12 22:55:23 CEST 2012


Thank you, it works.
by the way, sometime I guess *apply functions act like loop, because
when I apply a complicated function, they run slowly and one by one.

On Tue, Jun 12, 2012 at 3:39 PM, Rui Barradas <ruipbarradas at sapo.pt> wrote:
> Hello,
>
> Try
>
> sapply(seq_len(ncol(mt2)), function(j) mt1[mt2[, j], j])
>
>
> Hope this helps,
>
> Rui Barradas
>
> Em 12-06-2012 17:19, Zhenjiang Lan escreveu:
>
>> mt1:
>>      [,1] [,2] [,3]
>> [1,] "G"  "A"  "A"
>> [2,] "C"  "T"  "C"
>>
>> mt2:
>>      [,1] [,2] [,3]
>> [1,]    1    2    2
>> [2,]    2    1    2
>> [3,]    2    2    2
>> [4,]    1    1    1
>
>



More information about the R-help mailing list