[R] Getting elements of a matrix by a vector of column indice s
Liaw, Andy
andy_liaw at merck.com
Thu Jul 8 14:21:37 CEST 2004
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 at 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
>
>
More information about the R-help
mailing list