[R] subsetting a matrix
(Ted Harding)
Ted.Harding at nessie.mcc.ac.uk
Tue Sep 30 20:48:09 CEST 2003
On 30-Sep-03 Rajarshi Guha wrote:
> Hi,
> I'm trying to take a set of rows and columns out of a matrix. I hve
> been using the index aray approach. My overll matrix is X and is 179 x
> 65. I want to take out 4 columns and 161 rows.
>
> Thus I made a 161 x 2 array I and filled it up with the row,col
> indices.
> However doing,
>
> X[ I ] gives me a vector of the extracted elements. Is there anyway I
> can coerce this into a 161 x 4 matrix?
X[c(list_of_rows),c(list_of_cols)]
should do it. E.g.
X[c(2,5,7,9),c(3,5,6)]
Alternatively you can make index vectors iR (for rows), iC (for cols)
which have values TRUE for what you want to select and FALSE for the
rest; then
X[iR,iC]
will also do it. There are other approaches -- you should choose
whichever most closely fits in with your criteria for row and column
selection. E.g.
X[iR,c(3,5,6)]
would also do! What it all boils down to is an expression of the form
X[rowselector, colselector]
Hope this helps,
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 167 1972
Date: 30-Sep-03 Time: 19:48:09
------------------------------ XFMail ------------------------------
More information about the R-help
mailing list