[R] pairs matchning

chao gai chaogai at duineveld.demon.nl
Thu Oct 26 18:27:48 CEST 2006


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,]    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



More information about the R-help mailing list