[R] counts for grouped data

Thomas Lumley tlumley at u.washington.edu
Thu Sep 4 19:47:09 CEST 2003


On Thu, 4 Sep 2003, Paul Green wrote:

> How does one get counts for grouped data
> from ungrouped data. For example, how can
> I get
>
>          X1  X2  Count
>          1     1      1
>          1     2      2
>          2     1      1
>          2     2      1
>
> from
>
>          X1  X2
>          A    A
>          A    B
>          B    B
>          A    B
>          B    A
>

One possibility is

> df
  X1 X2
1  A  A
2  A  B
3  B  B
4  A  B
5  B  A
> aggregate(rep(1,nrow(df)), df, NROW)
  X1 X2 x
1  A  A 1
2  B  A 1
3  A  B 2
4  B  B 1


	-thomas




More information about the R-help mailing list