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? ------------------------------------------------------------------- Rajarshi Guha <rxg218 at psu.edu> <http://jijo.cjb.net> GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE ------------------------------------------------------------------- Disembowelling takes guts.
Rajarshi - Why not simply subscript your matrix X to return the rows and columns you want to keep ? For example, new <- X[16:176, c(3,5,7,9)] assuming those are the rows and columns you want. See help("Extract"). - tom blackwell - u michigan medical school - ann arbor - On Tue, 30 Sep 2003, Rajarshi Guha wrote:> 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? > > ------------------------------------------------------------------- > Rajarshi Guha <rxg218 at psu.edu> <http://jijo.cjb.net> > GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE > -------------------------------------------------------------------
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 ------------------------------