[R] retrieving matrix elements by giving pairs of row AND column numbers?

Werner Wernersen pensterfuzzer at yahoo.de
Wed Oct 22 20:23:51 CEST 2008


Hi,

this is probably a very trivial question but I can't figure out the right terms to find the solution in the list archive.

I have a matrix or a data.frame or the like:
> m <- matrix(ncol=3,seq(1,9))
> m
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

and now I wonder if one can provide somehow two vectors, one containing row numbers, the other column numbers and thereby retrieve the corresponding elements?
E.g., providing the 2 vectors c(1,2,3) and c(1,3,1) should return the matrix values c(1,8,3).

I ran a couple of times into this problem and always used some awkward transformations to get around it.
But as I know R, there is probably some slick way to do this.

Thanks!!!
  Werner







More information about the R-help mailing list