[R] Create a unique group id

Norbi Gurracho kumsaa at hotmail.com
Thu Jan 2 18:04:55 CET 2014


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: smartpink111 at yahoo.com
> Subject: Re: [R] Create a unique group id
> To: r-help at r-project.org
> CC: kumsaa at 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 <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