Skip to content

number of count of each unique row

4 messages · Louis Martin, G. Jay Kerns, Jim Lemon +1 more

1 day later
#
Hi Louis,

If I am understanding your question correctly, here is one way:
suppose your matrix is M of dimension n x k.

D <- data.frame(M)  # convert to data frame
ones <- rep(1, n)   # a column of 1s

Now you can count the number of repeats of each unique row.

aggregate( ones, by = as.list(D), FUN = sum)

The output will be a data frame with the unique rows, and a column at
the end labeled "x" with the frequency of each unique row.

Once you get this you can convert to a list, manipulate, etc.  I am
sure that there exist faster/better methods.

Best,
Jay
On Dec 21, 2007 5:03 PM, Louis Martin <louismartinbis at yahoo.fr> wrote:

  
    
#
Louis Martin wrote:
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
#
i didn't test, but i think you want something like:

table(apply(x, 1, paste, collapse=","))

where "x" is your matrix...

b
On Dec 23, 2007, at 5:01 AM, Jim Lemon wrote: