Hi Norbi, It would be almost the same.
vec1 <- with(mydf,paste(b,date)) res <- within(mydf, d <- match(vec1,unique(vec1))) res$d # [1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 5 mydf$date <- as.Date(mydf$date) A.K. On Thursday, January 2, 2014 12:04 PM, Norbi Gurracho <kum...@hotmail.com> wrote: Thanks Arun. How does code differs if I have a date variable instead of numbers like in column a? I have a sample data in dput output. > dput(mydf) structure(list(a = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("1", "2"), class = "factor"), b = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("1", "2", "3"), class = "factor"), c = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 5L), .Label = c("1", "2", "3", "4", "5"), class = "factor"), date = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("2010-01-01", "2010-01-02" ), class = "factor")), .Names = c("a", "b", "c", "date"), row.names = c(NA, -16L), class = "data.frame") Thanks ---------------------------------------- > Date: Thu, 2 Jan 2014 08:09:59 -0800 > From: smartpink...@yahoo.com > Subject: Re: [R] Create a unique group id > To: r-help@r-project.org > CC: kum...@hotmail.com > > 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 <smartpink...@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 <kum...@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@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.