[R] Excluding rows from a matrix

ben@zoo.ufl.edu ben at zoo.ufl.edu
Mon Aug 21 18:52:05 CEST 2000

On Mon, 21 Aug 2000, [iso-8859-1] José Ernesto Jardim wrote:

> Hi
> I have a matrix (4 x 950) and I want to remove 3 rows, where the values
> from the first column are 713, 714 and 715. I can select the rows, one
> by one, with
> mat[mat$first==713,]
> mat[mat$first==714,]
> ...
> but I'm unable to (i) select the 3 rows at once, (ii) select the matrix
> excluding those rows.

  The hard way (if you had non-contiguous rows): using "|" (logical or)
  mat[mat$first==713 | mat$first==714 | mat$first==715,]


  mat[mat$first>=713 & mat$first<=715,]

  If you were going to do a lot of this you could define a "between"

between <- function(x,lower,upper) {
  x>=lower & x<= upper


To get all the other rows use ! (not), for example


see the "Logical Operators" page in the R documentation.

  Ben Bolker

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list