[R] number of count of each unique row

Jim Lemon jim at bitwrit.com.au
Sun Dec 23 11:01:31 CET 2007


Louis Martin wrote:
> Hi,
> 
> I have a matrix of duplicate rows. How to output a list the unique rows with their count? I have used "unique" to have the unique rows, but can't produce the occurences of each unique row.
> 
Hi Louis,
If you want the unique rows returned, this might do the job.

unique.rows<-function(x) {
  nrows<-dim(x)[1]
  urows<-1:nrows
  for(i in 1:(nrows-1)) {
   for(j in (i+1):nrows) {
    if(!is.na(urows[j])) if(all(x[i,]==x[j,])) urows[j]<-NA
   }
  }
  return(x[urows[!is.na(urows)],])
}

Jim



More information about the R-help mailing list