[R] Counting

Petr Savicky savicky at praha1.ff.cuni.cz
Thu Mar 17 20:53:00 CET 2011


On Thu, Mar 17, 2011 at 02:54:49PM -0400, Jim Silverton wrote:
> I have a matrix say:
> 
> 23   1
> 12  12
> 0    0
> 0   1
> 0   1
> 0   2
> 23  2
> 
> I want to count of number of distinct rows and the number of disinct element
> in the second column and put these counts in a column. SO at the end of the
> day I should have:
> 
> c(1, 1, 1, 2, 2, 1, 1) for the distinct rows and c(1, 1, 1, 2, 2, 2, 2) for
> the counts of how many times the elements in the second column exists. Any
> help is greatly appreciated.

Hi.

I understand the first as follows. For each row compute the number of
rows, which are equal to the given one. If this is correct, then
the following can be used.

  a <- cbind(
  c(23, 12,  0,  0,  0,  0, 23),
  c(1, 12,  0,  1,  1,  2,  2))

  u <- rep(1, times=nrow(a))
  ave(u, a[, 1], a[, 2], FUN=sum)
  [1] 1 1 1 2 2 1 1

I am not sure, whether i understand the second correctly. Can you
explain it in more detail? I would expect

  ave(u, a[, 2], FUN=sum)
  [1] 3 1 1 3 3 2 2

However, this is different from your expected output. Do you count
only consecutive equal elements?

Hope this helps.

Petr Savicky.



More information about the R-help mailing list