RE: [R] Getting elements of a matrix by a vector of column indice s

From: Liaw, Andy <andy_liaw_at_merck.com>
Date: Thu 08 Jul 2004 - 22:21:37 EST


See if the following helps:

> m <- outer(letters[1:5], 1:4, paste, sep="")
> m

     [,1] [,2] [,3] [,4]

[1,] "a1" "a2" "a3" "a4"
[2,] "b1" "b2" "b3" "b4"
[3,] "c1" "c2" "c3" "c4"
[4,] "d1" "d2" "d3" "d4"
[5,] "e1" "e2" "e3" "e4"

> idx <- c(2, 1, 3, 4, 2)
> m[cbind(1:5, idx)]

[1] "a2" "b1" "c3" "d4" "e2"

Andy

> From: Wolfram Fischer
>
> 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
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
>



R-help@stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Jul 08 22:32:18 2004

This archive was generated by hypermail 2.1.8 : Fri 18 Mar 2005 - 02:35:15 EST