[R] Getting elements of a matrix by a vector of column indices

Wolfram Fischer wolfram at fischer-zim.ch
Thu Jul 8 11:58:02 CEST 2004


I have e.g.
    t <- matrix( nrow=2, ncol=3, byrow=TRUE, c('a1','a2','a3','b1','b2','b3') )
and
    i <- c( 3, 2)

Is it possible to formulate a simple expression that gets
    c( t[ 1, i[1] ], t[ 2, i[2] ] )
(and so on for longer matrices)?

The result would be:
    [1] "a3" "b2"

Thanks - Wolfram




More information about the R-help mailing list