Re: [R] Matching a vector with a matrix row

From: Petr Savicky <savicky_at_praha1.ff.cuni.cz>
Date: Sun, 24 Apr 2011 11:13:43 +0200

On Sat, Apr 23, 2011 at 08:56:33AM +0800, Luis Felipe Parra wrote:
> Hello Niels, I am trying to find the rows in Matrix which contain all of the
> elements in LHS.

This sounds like you want an equivalent of

  all(LHS %in% x)

However, in your original post, you used

  all(x %in% LHS)

What is correct?

If the equality of x and LHS should be tested, then try

   setequal(x, LHS)

If the rows may contain repeated elements and the number of repetitions should also match, then try

  identical(sort(x), sort(LHS))

with a precomputed sort(LHS) for efficiency.

If the number of the different character values in the whole matrix is not too large, then efficiency of the comparison may be improved, if the matrix is converted to a matrix consisting of integer codes instead of the original character values. See ?factor for the meaning of "integer codes". After this conversion, the comparison can be done by comparing integers instead of character values, which is faster.

Hope this helps.

Petr Savicky.



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Sun 24 Apr 2011 - 09:18:04 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 24 Apr 2011 - 19:30:33 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive