[R] Matching a vector with a matrix row
savicky at praha1.ff.cuni.cz
Sun Apr 24 11:13:43 CEST 2011
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
If the rows may contain repeated elements and the number of
repetitions should also match, then try
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.
More information about the R-help