Re: [R] Group by multiple variables

From: baptiste auguie <baptiste.auguie_at_googlemail.com>
Date: Tue, 31 May 2011 09:54:39 +1200

Hi,

There are probably much better ways, but try this

transform(dat, group = as.numeric(factor(paste(A,B,C, sep=""))))

HTH, baptiste

On 31 May 2011 09:47, Mendolia, Franco <fmendolia_at_mcw.edu> wrote:
> 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
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 30 May 2011 - 21:57:01 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 30 May 2011 - 22:40:10 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive