Two posts in one day is not a good day...and this question seems like it should have an obvious answer: I have a matrix where rows are unique combinations of 1's and 0's:> combs=as.matrix(expand.grid(c(0,1),c(0,1))) > combsVar1 Var2 [1,] 0 0 [2,] 1 0 [3,] 0 1 [4,] 1 1 I want a single function that will give the row index containing an exact match with vector x:> x=c(0,1)The solution needs to be applied many times, so I need something quick -- I was hoping a base function would do it, but I'm drawing a blank. Thanks! Kevin
On Jan 5, 2011, at 2:16 PM, Kevin Ummel wrote:> Two posts in one day is not a good day...and this question seems > like it should have an obvious answer: > > I have a matrix where rows are unique combinations of 1's and 0's: > >> combs=as.matrix(expand.grid(c(0,1),c(0,1))) >> combs > Var1 Var2 > [1,] 0 0 > [2,] 1 0 > [3,] 0 1 > [4,] 1 1 > > I want a single function that will give the row index containing an > exact match with vector x: > >> x=c(0,1)> intersect( which(combs[,1]==x[1]), which(combs[,2]==x[2]) ) [1] 3 Or maybe even faster: > which( combs[,1]==x[1] & combs[,2]==x[2]) [1] 3> > The solution needs to be applied many times, so I need something > quick -- I was hoping a base function would do it, but I'm drawing a > blank. >-- David Winsemius, MD West Hartford, CT
On Wed, Jan 05, 2011 at 07:16:47PM +0000, Kevin Ummel wrote:> Two posts in one day is not a good day...and this question seems like it should have an obvious answer: > > I have a matrix where rows are unique combinations of 1's and 0's: > > > combs=as.matrix(expand.grid(c(0,1),c(0,1))) > > combs > Var1 Var2 > [1,] 0 0 > [2,] 1 0 > [3,] 0 1 > [4,] 1 1 > > I want a single function that will give the row index containing an exact match with vector x: > > > x=c(0,1) > > The solution needs to be applied many times, so I need something quick -- I was hoping a base function would do it, but I'm drawing a blank.If the matrix can have different number of columns, then also the following can be used combs <- as.matrix(expand.grid(c(0,1),c(0,1),c(0,1))) x <- c(0,1,1) which(rowSums(combs != rep(x, each=nrow(combs))) == 0) # [1] 7 Petr Savicky.
Henrique Dallazuanna
2011-Jan-07 12:49 UTC
[R] Match numeric vector against rows in a matrix?
Try this: which(colSums(t(combs) == x) == ncol(combs)) On Wed, Jan 5, 2011 at 5:16 PM, Kevin Ummel <kevinummel@gmail.com> wrote:> Two posts in one day is not a good day...and this question seems like it > should have an obvious answer: > > I have a matrix where rows are unique combinations of 1's and 0's: > > > combs=as.matrix(expand.grid(c(0,1),c(0,1))) > > combs > Var1 Var2 > [1,] 0 0 > [2,] 1 0 > [3,] 0 1 > [4,] 1 1 > > I want a single function that will give the row index containing an exact > match with vector x: > > > x=c(0,1) > > The solution needs to be applied many times, so I need something quick -- I > was hoping a base function would do it, but I'm drawing a blank. > > Thanks! > Kevin > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Hi Petr, Sorry for the delay. I ended up implementing a simple and fast but not-universal or elegant solution, since it turned out that I only needed to test a rather small subset of combinations for my purposes. Nevertheless, I did go back and try the solutions posted. Bill's 'binary expansion' approach was the fastest by about a factor of 8. Cheers, Kevin On Jan 11, 2011, at 9:20 AM, Petr Savicky wrote:> Dear Kevin Ummel: > > There were several suggestions on R-help concering your question below. > None of them suggests a base function. I would also expect that > there is a base function for matching the rows. A related function is > dist(), but it takes only one matrix as input and computes the distances > between all pairs of its rows. Matching only some rows would require > a modification of dist(), which would take two matrices and compare > rows of one of them to the rows of the other. However, i do not know > such a function in R. > > Is some of the suggestions on R-help suitable for your purposes? > > Thank you in advance for your kind reply. > > Best regards, Petr Savicky. > > On Wed, Jan 05, 2011 at 07:16:47PM +0000, Kevin Ummel wrote: >> Two posts in one day is not a good day...and this question seems like it should have an obvious answer: >> >> I have a matrix where rows are unique combinations of 1's and 0's: >> >>> combs=as.matrix(expand.grid(c(0,1),c(0,1))) >>> combs >> Var1 Var2 >> [1,] 0 0 >> [2,] 1 0 >> [3,] 0 1 >> [4,] 1 1 >> >> I want a single function that will give the row index containing an exact match with vector x: >> >>> x=c(0,1) >> >> The solution needs to be applied many times, so I need something quick -- I was hoping a base function would do it, but I'm drawing a blank. >> >> Thanks! >> Kevin >> >> ______________________________________________ >> 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. >>