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

Jason J. Pitt pittjj at uchicago.edu
Sat Jan 28 08:10:15 CET 2012


Hi Melissa,

Well, assuming you know the length of the length of the row in 
the matrix and vector are the same... if you need a quick fix 
you could use

mine <- 1:6
table.combos <- matrix(data = 1:12, nrow = 2, ncol = 6, byrow 
= T)
 
sum(mine == table.combos[1, ]) == length(mine)
# returns TRUE
sum(mine == table.combos[2, ]) == length(mine)
#returns FALSE

Though I'm sure there are more elegant and less error prone 
(if you don't know the lengths are equal) ways of doing this 
and others will point you in the right direction.

Jason

---- Original message ----
>Date: Fri, 27 Jan 2012 17:39:05 -0800 (PST)
>From: r-help-bounces at r-project.org (on behalf of Melrose2012 
<melissa.patrician at stonybrook.edu>)
>Subject: [R] finding rows in a matrix that match a vector  
>To: r-help at r-project.org
>
>Hi,
>
>Please excuse my ignorance, 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 osmething
>like this, except I don't know what function I am supposed to 
input into
>'apply' to use this.
>
>Thanks in advance for the help!
>
>Cheers,
>Melissa
>
>--
>View this message in context: 
http://r.789695.n4.nabble.com/finding-rows-in-a-matrix-that-
match-a-vector-tp4335216p4335216.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>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.



More information about the R-help mailing list