[R] Data Grouping Question

Gabor Grothendieck ggrothendieck at myway.com
Tue Mar 30 22:53:52 CEST 2004


Kissell, Robert [EQRE] <robert.kissell <at> citigroup.com> writes:

> I would like to learn how I can group the data on unique rows of A and also 
count the number of times the row
> occurred. 

You have already provided the answer, unique(A), to the first part of your
question.  Here are two solutions to the second part:

1. Since each row can be regarded as the representation of a binary
number:

 table(A%*%2^(0:3))

2. Another possibility not dependent on the binary nature of the
data is to define:

 "%+.*%" <- function(a,b)apply(b,2,function(x)apply(t(a) == x,2,all))

This function is the +.x of APL.  It defines an infix function that does a
matrix multiply of matrix a and matrix b except it replaces the usual inner
product of two vectors x and y with all(x==y).

In terms of this function, the answer is:

 colSums( A %+.*% t(unique(A)) )




More information about the R-help mailing list