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

From: Luis Felipe Parra <felipe.parra_at_quantil.com.co>
Date: Sat, 23 Apr 2011 08:56:33 +0800

Hello Niels, I am trying to find the rows in Matrix which contain all of the elements in LHS.

Thank you

Felipe Parra

On Fri, Apr 22, 2011 at 10:30 PM, Niels Richard Hansen < Niels.R.Hansen+lists_at_math.ku.dk> wrote:

> Joshua and Luis
>
> Neither of you is exactly solving the problem as stated, see
> below. Luis, could you clarify if you want rows that are _equal_
> to a vector or rows with entries _contained_ in a vector?
>
> If
>
> m <- matrix(c("A", "B", "C", "B", "A", "A"), 3, 2)
> LHS <- c("A", "B")
>
> then LHS equals the first row only, while
>
> apply(m, 1, function(x) all(x %in% LHS))
> [1] TRUE TRUE FALSE
>
> finds the rows with entries contained in LHS and
>
> which(m %in% LHS)
> [1] 1 2 4 5 6
>
> finds all entries in m that equals an entry in LHS. While
> you can turn the latter into the former, this will have some
> computational costs too. The R-code
>
> apply(m, 1, function(x) all(x == LHS))
> [1] TRUE FALSE FALSE
>
> finds the rows that are equal to LHS.
>
> - Niels
>
>
> On 22/04/11 00.18, Joshua Wiley wrote:
>
>> Hi Felipe,
>>
>> 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
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>>
>>
>>
>>
> --
> Niels Richard Hansen Web: www.math.ku.dk/~richard
> Associate Professor Email: Niels.R.Hansen_at_math.ku.dk
> Department of Mathematical Sciences
> nielsrichardhansen_at_gmail.com
> University of Copenhagen Skype: nielsrichardhansen.dk
> Universitetsparken 5 Phone: +1 510 502 8161
> 2100 Copenhagen Ø
> Denmark
>
>
>
>
>
>
>
>

[[alternative HTML version deleted]]

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 Sat 23 Apr 2011 - 00:59:27 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 - 10:20: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.