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

From: Joshua Wiley <jwiley.psych_at_gmail.com>
Date: Thu, 21 Apr 2011 21:18:29 -0700

Since matrices are just a vector with dimensions, you could easily use something like this (which at least on my system, is slightly faster):

results <- which(Matrix %in% LHS)

I'm not sure this is the fastest technique thought. It will return a vector of the positions in "Matrix" that match "LHS". You can easily convert to row numbers if you want since all columns have the same number of rows.

HTH, Josh

On Thu, Apr 21, 2011 at 8:56 PM, Luis Felipe Parra <felipe.parra_at_quantil.com.co> wrote:
> Hello I am trying to compare a vector with a Matrix's rows.The vector has
> the same length as the number of columns of the matrix, and I would like to
> find the row numbers where the matrix's row us the same as the given vector.
> What I am doing at the moment is using apply as follows:
>
> apply(Matrix,1,function(x)all(x%in%LHS))
>
> but this isn't too fast actually. I would like  to know if any body knows an
> efficient (fast) way of doing this? The matrix contains stings (not
> numbers).
>
> Thank you
>
> Felipe Parra
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

```--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help