[R] pairs matchning

TobiasBr at Taquanta.com TobiasBr at Taquanta.com
Thu Oct 26 18:12:48 CEST 2006


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,]    1    2
 [2,]    1    2
 [3,]    1    2
 [4,]    2    2
 [5,]    1    1
 [6,]    1    2
 [7,]    1    2
 [8,]    1    1
 [9,]    1    2
[10,]    1    1
> (B <- unique(A))
     [,1] [,2]
[1,]    1    2
[2,]    2    2
[3,]    1    1
> 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,]    1    2
 [2,]    1    2
 [3,]    1    2
 [4,]    2    2
 [5,]    1    1
 [6,]    1    2
 [7,]    1    2
 [8,]    1    1
 [9,]    1    2
[10,]    1    1
> 

HTH


>-----Original Message-----
>From: r-help-bounces at stat.math.ethz.ch 
>[mailto:r-help-bounces at stat.math.ethz.ch] 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,]    1    0
>[2,]    3    4
>[3,]    3    4
>[4,]    5    8
>[5,]    1    0
>[6,]    1    0
>[7,]    6    7
>
>Matrix (2) contains the *unique* pairs:
>
>      [,1] [,2]
>[1,]    1    0
>[2,]    3    4
>[3,]    5    8
>[4,]    6    7
>
>
>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 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
>and provide commented, minimal, self-contained, reproducible code.
>

********************
Nedbank Limited Reg No 1951/000009/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 ]



More information about the R-help mailing list