Skip to content
Prev 237496 / 398500 Next

Counting unique items in a list of matrices

On 2010-10-07 10:10, Jim Silverton wrote:
Here's one way, using the plyr package:

  require(plyr)
  ## make a list of 2X2 matrices
  L <- vector('list', 5000)
  set.seed(4321)
  for(i in 1:5000) L[[i]] <- matrix(round(runif(4), 1), 2, 2)

  ## convert each matrix to a string of 4 numbers, then
  ## form dataframe
  dL <- ldply(L, function(.x) toString(unlist(.x)))

  ## add an index vector
  dL$ind <- seq_len(5000)

  ## count unique strings; return string, frequency, indeces
  result <- ddply(dL, .(V1), summarize,
                               freq=length(V1),
                               idx=toString(ind))


    -Peter Ehlers