Hi, May be this also helps:
dat2<-within(dat1,{group<-as.numeric(factor(paste0(dim1,dim2,dim3)))}) head(dat2) # var dim1 dim2 dim3 group #1 0.5366483 x 1 1 1 #2 0.3081562 y 2 2 17 #3 0.1493687 z 1 3 23 #4 0.3202687 x 2 4 9 #5 0.1177976 y 1 5 15 #6 0.7709756 z 2 1 26 A.K. ----- Original Message ----- From: Francesco Sarracino <f.sarrac...@gmail.com> To: r-help@r-project.org Cc: Sent: Monday, December 10, 2012 9:33 AM Subject: [R] equivalent of group command of the egen function in Stata Dear R listers, I am trying to create a new variable that uniquely identifies groups of observations in a dataset. So far I couldn't figure out how to do this in R. In Stata I would simply type: egen newvar = group(dim1, dim2, dim3) Please, find below a quick example to show what I am dealing with: I have a dataset with 4 variables: var <- runif(50) ## a variable that I want to group dim1 <- factor(rep(1:3, length.out= 50), labels = c("x","y","z") ) ## 3 variables that should form the groups dim2 <- rep(1:2, length.out= 50) dim3 <- rep(1:5, length.out= 50) data <- data.frame(var, dim1, dim2, dim3) I am trying to build a fifth one (let's say: group_id) to uniquely identify groups of observations as defined by dim1, dim2 and dim3, i.e. 30 groups. can you please help me figuring out how to do it? thanks in advance, f. -- Francesco Sarracino, Ph.D. https://sites.google.com/site/fsarracino/ [[alternative HTML version deleted]] ______________________________________________ R-help@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@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.