[R] Create a unique group id

arun smartpink111 at yahoo.com
Thu Jan 2 17:09:59 CET 2014


Hi,

Also, to make it general:

vec1 <- with(dat,paste(a,b))
 within(dat,d <- as.numeric(factor(vec1,labels=seq(length(unique(vec1))))))

#or
 within(dat,d <- match(vec1,unique(vec1)))


#or
within(dat,d<- as.numeric(interaction(a,b))) #ids are unique, not in the same order
A.K.




On Thursday, January 2, 2014 9:30 AM, arun <smartpink111 at yahoo.com> wrote:
Hi,
Try:
dat <- read.table(text="a    b    c
 1    1    1
 1    1    1
 1    1    1
 1    2    2
 1    2    2
 1    2    2
 1    3    3
 1    3    3
 1    3    3
 2    1    4
 2    1    4
 2    1    4
 2    2    5
 2    2    5
 2    2    5
 2    2    5",sep="",header=TRUE)                          


  within(dat, d <- as.numeric(factor(paste(a,b),labels=1:5)))
A.K.



On Thursday, January 2, 2014 8:27 AM, Norbi Gurracho <kumsaa at hotmail.com> wrote:
I have a following sample data frame. How can I create a group id of column and b and to obtain column c? a    b    c 1    1    1 1    1    1 1    1    1 1    2    2 1    2    2 1    2    2 1    3    3 1    3    3 1    3    3 2    1    4 2    1    4 2    1    4 2    2    5 2    2    5 2    2    5 2    2    5                            
    [[alternative HTML version deleted]]

______________________________________________
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.





More information about the R-help mailing list