Thursday 15 September 2011

Index value for matrix in R? -


Is there a function to get the index for the matrix (line number and column number)?

Say I have a simple matrix:

  a < - For example, the number "23" is an easy way to get something back like C (3, 5) for the matrix (1:50, nero = 5)  

This works, but I'm sure this is a better way:

pre> matrix.andx < - Function (a, value) {idx & lt; - which (data.frame (a) == value) col.num & lt; - Roof (idx / nrow (a)) line .Num & lt; - idx - (col.num-1) * nrow (a) return (c (row.num, col.num)) & gt; Matrix.indx (A, 23) [1] 3 5> Matrix. Andx (A, 50) [1] 5 10

Just seen Help for () after posting and got the answer: arr.ind parameter.

  which (a == 23, arr.ind = true) line Col [1,] 3 5  

No comments:

Post a Comment