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

From: Wolfram Fischer <wolfram_at_fischer-zim.ch>
Date: Fri 09 Jul 2004 - 00:26:00 EST

Thanks for you answer! It works.

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

The following works with the help of your proposition:
> rowidx.n <- c( 2, 3, 4)
> colidx.n <- c( 1, 3, 2)
> idx.n <- cbind( rowidx.n, colidx.n )
> m[idx.n]

[1] "b1" "c3" "d2"

In my real data there was an additional difficulty: I had names of rows and columns as indices:
> rownames(m) <- paste('R', 1:nrow(m), sep="")
> colnames(m) <- paste('C', 1:ncol(m), sep="" )

And the following did not work anymore:
> rowidx <- c( 'R2', 'R3', 'R4' )
> colidx <- c( 'C1', 'C3', 'C2' )
> idx <- cbind( rowidx, colidx )
> m[idx]

<NA> <NA> <NA> <NA> <NA> <NA>
  NA NA NA NA NA NA

Do you have another suggestion? - 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 Received on Fri Jul 09 00:37:28 2004

This archive was generated by hypermail 2.1.8 : Wed 03 Nov 2004 - 22:54:47 EST