[R] finding rows in a matrix that match a vector

Uwe Ligges ligges at statistik.tu-dortmund.de
Thu Feb 2 12:16:01 CET 2012



On 28.01.2012 05:43, Melissa Patrician wrote:
> Hi,
>
> Please excuse my inexperience, but I am just learning R (this is my very
> first day programming in R) and having a really hard time figuring out
> how to do the following:
>
> I have a matrix that is 1000 row by 6 columns (named 'table.combos') and
> a 1 row by 6 column vector (named 'mine').  I want to find every row in
> 'table.combos' that equals 'mine' and then count this number of times
> that this is the case.
>
> In matlab, I would use the 'find' command but I can not seem to figure
> out what syntax to use for R.
>
> Can anyone please help?
>
> Again, I'm assuming this is probably a very easy thing to do, but since
> I am new to R, I am having a hard time figuring it out.  I did some
> research on previous posts and saw that the 'apply' function appears to
> do something like this, but I don't know what function I am supposed to
> input into 'apply' to use this.
>

If you like the apply way, you have a vector v and your matrix M:

sum(apply(table.combos, 1, function(x) all(x == mine)))

Uwe Ligges


> Thanks in advance for the help!
>
> Cheers,
> Melissa
>



More information about the R-help mailing list