Re: [R] Count unique rows/columns in a matrix

From: Gabor Csardi <csardi_at_rmki.kfki.hu>
Date: Sat, 12 Jan 2008 22:17:14 +0100

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

______________________________________________
R-help_at_r-project.org 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.
Received on Sat 12 Jan 2008 - 21:21:55 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 12 Jan 2008 - 22:30:06 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive