[R] Group by multiple variables

Mendolia, Franco fmendolia at mcw.edu
Mon May 30 23:47:45 CEST 2011


Hello,

I would like to create a group variable that is based on the values of three variables:

For example,

> dat <- data.frame(A=c(1,1,1,1,1,2,2,2,2,2),
                    B=c(1,1,1,5,5,5,9,9,9,9),
                    C=c(1,1,1,1,1,2,2,7,7,7))
> dat
   A B C
 1  1 1 1
 2  1 1 1
 3  1 1 1
 4  1 5 1
 5  1 5 1
 6  2 5 2
 7  2 9 2
 8  2 9 7
 9  2 9 7
10 2 9 7

All rows that that are equal, belong to the same group, that is I would like to create a group variable like this:

   A B C group
 1  1 1 1   1
 2  1 1 1   1
 3  1 1 1   1
 4  1 5 1   2
 5  1 5 1   2
 6  2 5 2   3
 7  2 9 2   4
 8  2 9 7   5
 9  2 9 7   5
10 2 9 7   5

Is there an easy way to do this? Right now I use a bunch of loops and that seems rather cumbersome. In general, my data set is rather large and the number of categories per variable is not always fixed.

Franco


More information about the R-help mailing list