You said you wanted the equivalent of the Unix 'uniq -c' but said
that xtab's results were roughly right and the rle might be what
you want.  rle() is the equivalent of 'uniq -c', they both output the
lengths of runs of identical elements.   if the data is sorted they
are equivalent to using table() or xtabs().

Since you have sorted data try the following

isFirstInRun <- function(x) UseMethod("isFirstInRun")
isFirstInRun.default <- function(x) c(TRUE, x[-1] != x[-length(x)])
isFirstInRun.data.frame <- function(x) {
    stopifnot(ncol(x)>0)
    retval <- isFirstInRun(x[[1]])
    for(column in x) {
        retval <- retval | isFirstInRun(column)
    }
    retval
}

i <- which(isFirstInRun(yourDataFrame))

Then I think
   data.frame(Count=diff(c(i, 1L+nrow(yourDataFrame))), yourDataFrame[i,])
gives you what you want.   E.g.,
  > yourDataFrame <- data.frame(x1=c(1,1,2,2,1), x2=c(11,11,11,12,11))
  > i <- which(isFirstInRun(yourDataFrame))
  > i
  [1] 1 3 4 5
  > data.frame(Count=diff(c(i, 1L+nrow(yourDataFrame))), yourDataFrame[i,])
    Count x1 x2
  1     2  1 11
  3     1  2 11
  4     1  2 12
  5     1  1 11

It should be pretty quick.  If you have missing values in your data frame,
you will have to make some decisions about whether they should be
considered equal to each other or not and modify isFirstInRun.default
accordingly.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
> Behalf
> Of Sam Steingold
> Sent: Tuesday, October 16, 2012 10:46 AM
> To: r-help@r-project.org; Duncan Murdoch
> Subject: Re: [R] uniq -c
> 
> > * Duncan Murdoch <zheqbpu.qha...@tznvy.pbz> [2012-10-16 12:47:36 -0400]:
> >
> > On 16/10/2012 12:29 PM, Sam Steingold wrote:
> >> x is sorted.
> > sparseby(data=x, INDICES=x, FUN=nrow)
> 
> this takes forever; apparently, it does not use the fact that x is
> sorted (even then - it should not take more than a few minutes)...
> 
> --
> Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 
> 11.0.11103000
> http://www.childpsy.net/ http://openvotingconsortium.org 
> http://www.memritv.org
> http://think-israel.org http://pmw.org.il http://thereligionofpeace.com
> Save the whales, feed the hungry, free the mallocs.
> 
> ______________________________________________
> R-help@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.

______________________________________________
R-help@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.

Reply via email to