[R] Tagging identical rows of a matrix

Gabor Grothendieck ggrothendieck at myway.com
Fri May 14 21:47:56 CEST 2004



The shortest expression I can think of is:

as.numeric(interaction(mat[,1],mat[,2],drop=T))

Completing the thought of those who suggested dist or
unique:

N <- nrow(mat)
dd <- as.matrix(dist(rbind(mat,unique(mat))))[-seq(N),seq(N)]
apply(dd,2,function(x)match(0,x))


Scott Waichler <scott.waichler <at> pnl.gov> writes:

: 
: I would like to generate a vector having the same length
: as the number of rows in a matrix.  The vector should contain
: an integer indicating the "group" of the row, where identical
: matrix rows are in a group, and a unique row has a unique integer.
: Thus, for
: 
: a <- c(1,2)
: b <- c(1,3)
: c <- c(1,2)
: d <- c(1,2)
: e <- c(1,3)
: f <- c(2,1)
: mat <- rbind(a,b,c,d,e,f)
: 
: I would like to get the vector c(1,2,1,1,2,3).  I know dist() gives
: part of the answer, but I can't figure out how to use it for
: this purpose without doing a lot of looping.  I need to apply this
: to matrices up to ~100000 rows.
: 
: Thanks,
: Scott Waichler
: Pacific Northwest National Laboratory
: scott.waichler_at_pnl.gov




More information about the R-help mailing list