[R] pairs matchning
Hi All, I have two numerical matrices of 2 columns and many rows. The two coulumns of matrix (1) form a number of 'pairs' of numbers, e.g: [,1] [,2] [1,]10 [2,]34 [3,]34 [4,]58 [5,]10 [6,]10 [7,]67 Matrix (2) contains the *unique* pairs: [,1] [,2] [1,]10 [2,]34 [3,]58 [4,]67 I would like to create a vector matching the pairs in matrix (1) to the unique pairs in matrix (2), e.g: [1] 1 2 2 3 1 1 4 (done by hand) match() does not seem to be able to handle pairs, and I don't seem to be able to find an elegant solution... Cheers, Federico -- Federico C. F. Calboli Department of Epidemiology and Public Health Imperial College, St Mary's Campus Norfolk Place, London W2 1PG Tel +44 (0)20 7594 1602 Fax (+44) 020 7594 3193 f.calboli [.a.t] imperial.ac.uk f.calboli [.a.t] gmail.com __ R-help@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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] pairs matchning
Hi You could try to find an equivalent representation as a string and try to match those. (A - cbind(sample(1:2, 10, rep=TRUE), sample(1:2, 10, rep=TRUE))) [,1] [,2] [1,]12 [2,]12 [3,]12 [4,]22 [5,]11 [6,]12 [7,]12 [8,]11 [9,]12 [10,]11 (B - unique(A)) [,1] [,2] [1,]12 [2,]22 [3,]11 strRep - function(mat) apply(mat, 1, function(x) paste(x, collapse=;)) strRep(B) [1] 1;2 2;2 1;1 (idx - match(strRep(A), strRep(B))) [1] 1 1 1 2 3 1 1 3 1 3 B[idx, ] [,1] [,2] [1,]12 [2,]12 [3,]12 [4,]22 [5,]11 [6,]12 [7,]12 [8,]11 [9,]12 [10,]11 HTH -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Federico Calboli Sent: 26 October 2006 06:48 PM To: r-help Subject: [R] pairs matchning Hi All, I have two numerical matrices of 2 columns and many rows. The two coulumns of matrix (1) form a number of 'pairs' of numbers, e.g: [,1] [,2] [1,]10 [2,]34 [3,]34 [4,]58 [5,]10 [6,]10 [7,]67 Matrix (2) contains the *unique* pairs: [,1] [,2] [1,]10 [2,]34 [3,]58 [4,]67 I would like to create a vector matching the pairs in matrix (1) to the unique pairs in matrix (2), e.g: [1] 1 2 2 3 1 1 4 (done by hand) match() does not seem to be able to handle pairs, and I don't seem to be able to find an elegant solution... Cheers, Federico -- Federico C. F. Calboli Department of Epidemiology and Public Health Imperial College, St Mary's Campus Norfolk Place, London W2 1PG Tel +44 (0)20 7594 1602 Fax (+44) 020 7594 3193 f.calboli [.a.t] imperial.ac.uk f.calboli [.a.t] gmail.com __ R-help@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 and provide commented, minimal, self-contained, reproducible code. Nedbank Limited Reg No 1951/09/06. The following link displays the names of the Nedbank Board of Directors and Company Secretary. [ http://www.nedbank.co.za/terms/DirectorsNedbank.htm ] This email is confidential and is intended for the addressee only. The following link will take you to Nedbank's legal notice. [ http://www.nedbank.co.za/terms/EmailDisclaimer.htm ] __ R-help@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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] pairs matchning
Federico, Why not 1 convert to data frame dfo - as.data.frame(mat) 2 take unique items with ?unique df- unique(dfo) 3 add an index to the unique itmes df$unique - 1:nrow(df) 4 merge the two data.frames combi - merge(dfo,df) 5 extract the index? combi$unique Kees On Thursday 26 October 2006 18:47, Federico Calboli wrote: Hi All, I have two numerical matrices of 2 columns and many rows. The two coulumns of matrix (1) form a number of 'pairs' of numbers, e.g: [,1] [,2] [1,]10 [2,]34 [3,]34 [4,]58 [5,]10 [6,]10 [7,]67 Matrix (2) contains the *unique* pairs: [,1] [,2] [1,]10 [2,]34 [3,]58 [4,]67 I would like to create a vector matching the pairs in matrix (1) to the unique pairs in matrix (2), e.g: [1] 1 2 2 3 1 1 4 (done by hand) match() does not seem to be able to handle pairs, and I don't seem to be able to find an elegant solution... Cheers, Federico __ R-help@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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] pairs matchning
If all of the numbers are one-digit numbers, as your subset shows, you can create a third column equal to 10 * col 1 + col 2, and then use match() on the resulting two-digit numbers, no? If the numbers are larger than one-digit, and you know the maximum, use that rather than 10 as the multiplier. Ben -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Federico Calboli Sent: Thursday, October 26, 2006 11:48 AM To: r-help Subject: [R] pairs matchning Hi All, I have two numerical matrices of 2 columns and many rows. The two coulumns of matrix (1) form a number of 'pairs' of numbers, e.g: [,1] [,2] [1,]10 [2,]34 [3,]34 [4,]58 [5,]10 [6,]10 [7,]67 Matrix (2) contains the *unique* pairs: [,1] [,2] [1,]10 [2,]34 [3,]58 [4,]67 I would like to create a vector matching the pairs in matrix (1) to the unique pairs in matrix (2), e.g: [1] 1 2 2 3 1 1 4 (done by hand) match() does not seem to be able to handle pairs, and I don't seem to be able to find an elegant solution... Cheers, Federico -- Federico C. F. Calboli Department of Epidemiology and Public Health Imperial College, St Mary's Campus Norfolk Place, London W2 1PG Tel +44 (0)20 7594 1602 Fax (+44) 020 7594 3193 f.calboli [.a.t] imperial.ac.uk f.calboli [.a.t] gmail.com __ R-help@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 and provide commented, minimal, self-contained, reproducible code. __ R-help@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 and provide commented, minimal, self-contained, reproducible code.