Antje Döring
2005-Nov-21 15:57 UTC
[R] Comparing rows of matrices with different dimensions
Hi there, I have a question, which I thought is very easy to solve, but somehow I can't find a solution. Probably someone could help me quickly? Here it is: I have two matrices: a [,1] [,2] [,3] [1,] 1 4 9 [2,] 2 6 10 [3,] 3 6 11 [4,] 4 8 12 b [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 Now I want to find out which rows of b can also be found in a (without its last column). So the solution must be something like either "TRUE FALSE TRUE" or the rows where their is a match (rows 1 and 3) Till now I have tried things like b %in% a[,1:2] or so but that doesn't work because I want to compare the WHOLE row of b with the whole row of a without column 3. Thank you very much for any help. Regards, Antje [[alternative HTML version deleted]]
Dimitris Rizopoulos
2005-Nov-21 16:13 UTC
[R] Comparing rows of matrices with different dimensions
how about: a <- cbind(1:4, c(4, 6, 6, 8), 9:12) b <- cbind(1:3, 4:6) ##### apply(b, 1, paste, collapse = "") %in% apply(a[, -3], 1, paste, collapse = "") I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://www.med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Antje D??ring" <Antje.Doering at komdat.com> To: <r-help at stat.math.ethz.ch> Sent: Monday, November 21, 2005 4:57 PM Subject: [R] Comparing rows of matrices with different dimensions> > > Hi there, > > > > I have a question, which I thought is very easy to solve, but > somehow I can't find a solution. Probably someone could help me > quickly? > > > > Here it is: > > > > I have two matrices: > > > > a > > [,1] [,2] [,3] > > [1,] 1 4 9 > > [2,] 2 6 10 > > [3,] 3 6 11 > > [4,] 4 8 12 > > > > > > b > > [,1] [,2] > > [1,] 1 4 > > [2,] 2 5 > > [3,] 3 6 > > > > Now I want to find out which rows of b can also be found in a > (without its last column). So the solution must be something like > either "TRUE FALSE TRUE" or the rows where their is a match (rows 1 > and 3) > > > > Till now I have tried things like b %in% a[,1:2] or so but that > doesn't work because I want to compare the WHOLE row of b with the > whole row of a without column 3. > > > > Thank you very much for any help. > > > > Regards, Antje > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Berton Gunter
2005-Nov-21 16:24 UTC
[R] Comparing rows of matrices with different dimensions
If I understand you correctly, ## not tested apply(cbind(a[,-3],b),1,function(x)isTRUE(all.equal(x[1:2],x[3:4]))) or something similar. The basic idea is just to drop the last column of a and check to see whether rows are the same (possible within numeric fuzz). This assumes order counts. If the rows must just contain the same values (possibly replicated different numbers of times, then add calls to unique (or maybe use setdiff if numeric fuzz is not an issue). HTH Cheers, Bert -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Antje D??ring > Sent: Monday, November 21, 2005 7:57 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Comparing rows of matrices with different dimensions > > > > Hi there, > > > > I have a question, which I thought is very easy to solve, but > somehow I can't find a solution. Probably someone could help > me quickly? > > > > Here it is: > > > > I have two matrices: > > > > a > > [,1] [,2] [,3] > > [1,] 1 4 9 > > [2,] 2 6 10 > > [3,] 3 6 11 > > [4,] 4 8 12 > > > > > > b > > [,1] [,2] > > [1,] 1 4 > > [2,] 2 5 > > [3,] 3 6 > > > > Now I want to find out which rows of b can also be found in a > (without its last column). So the solution must be something > like either "TRUE FALSE TRUE" or the rows where their is a > match (rows 1 and 3) > > > > Till now I have tried things like b %in% a[,1:2] or so but > that doesn't work because I want to compare the WHOLE row of > b with the whole row of a without column 3. > > > > Thank you very much for any help. > > > > Regards, Antje > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
Marc Schwartz (via MN)
2005-Nov-21 16:41 UTC
[R] Comparing rows of matrices with different dimensions
On Mon, 2005-11-21 at 16:57 +0100, Antje D伱伓ring wrote:> > Hi there, > > > > I have a question, which I thought is very easy to solve, but somehow > I can't find a solution. Probably someone could help me quickly? > > > > Here it is: > > > > I have two matrices: > > > > a > > [,1] [,2] [,3] > > [1,] 1 4 9 > > [2,] 2 6 10 > > [3,] 3 6 11 > > [4,] 4 8 12 > > > > > > b > > [,1] [,2] > > [1,] 1 4 > > [2,] 2 5 > > [3,] 3 6 > > > > Now I want to find out which rows of b can also be found in a (without > its last column). So the solution must be something like either "TRUE > FALSE TRUE" or the rows where their is a match (rows 1 and 3) > > > > Till now I have tried things like b %in% a[,1:2] or so but that > doesn't work because I want to compare the WHOLE row of b with the > whole row of a without column 3. > > > > Thank you very much for any help. > > > > Regards, AntjeHere is one possible approach, though not tested beyond this example:> which(apply(matrix(b %in% a, dim(b)), 1, all))[1] 1 3 The steps are: # which elements of b are in a> b %in% a[1] TRUE TRUE TRUE TRUE FALSE TRUE # Turn that into a matrix the same shape as b> matrix(b %in% a, dim(b))[,1] [,2] [1,] TRUE TRUE [2,] TRUE FALSE [3,] TRUE TRUE # get T/F for which rows are all TRUE> apply(matrix(b %in% a, dim(b)), 1, all)[1] TRUE FALSE TRUE # Now get the indices> which(apply(matrix(b %in% a, dim(b)), 1, all))[1] 1 3 HTH, Marc Schwartz