[R] subsetting a matrix

Jason Turner jasont at indigoindustrial.co.nz
Tue Sep 30 21:51:01 CEST 2003

(Ted Harding) wrote:

> On 30-Sep-03 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.
This is documented in "An Introduction to R", under the section "Arrays 
and Matricies".  There's lots of good stuff in there.  From R:


And click on "An Introduction to R".

The short answer to this particular question:
a) negative indicies remove rows or columns.  Since you only want to 
remove 4 columns, I'd use that.
b) Since you only want to keep 18 rows, I'd use the numbers of the rows 
you want to keep.

As a toy example, say you wanted to remove columns 51 to 54, and keep 
the last 18 rows:

newX <- X[162:179,-(51:54)]

There's even more handy stuff in the document mentioned above.


Indigo Industrial Controls Ltd.
jasont at indigoindustrial.co.nz

More information about the R-help mailing list