[R] Count unique rows/columns in a matrix
Gabor Csardi
csardi at rmki.kfki.hu
Sat Jan 12 22:17:14 CET 2008
Chuck, thanks a lot, this is a very good starting point.
G.
On Sat, Jan 12, 2008 at 01:08:11PM -0800, Charles C. Berry wrote:
[...]
> Gabor,
>
> Try this. Order the matrix rows, conpare adjacent rows, and run length
> encode the logical vector of comparisons. Decode the rle() result to get
> the counts, use the logical vector comparing adjacent rows to identify the
> unique rows, and cbind() them together. Like this:
>
> count.rows <-
> function(x)
> {
> order.x <- do.call(order,as.data.frame(x))
> equal.to.previous <-
> rowSums(x[tail(order.x,-1),] != x[head(order.x,-1),])==0
> tf.runs <- rle(equal.to.previous)
> counts <- c(1,
> unlist(mapply( function(x,y) if (y) x+1 else (rep(1,x)),
> tf.runs$length, tf.runs$value )))
> counts <- counts[ c(diff(counts) <= 0, TRUE ) ]
> unique.rows <- which( c(TRUE, !equal.to.previous ) )
> cbind( counts, x[order.x[ unique.rows ], ,drop=F] )
> }
>
> HTH,
>
> Chuck
>
[...]
--
Csardi Gabor <csardi at rmki.kfki.hu> UNIL DGM
More information about the R-help
mailing list