You don't really need to do all of that, since you can use group directly. But if you want to have a numeric value for some reason, here's one possible way:
group <- c("A", "A", "A", "A", "B", "B", "B", "C", "C", "C", "C", "C") id_group <- as.numeric(as.factor(group)) correlative_group <- unlist(lapply(rle(group)$lengths, function(x)seq(1, x, by=1))) cbind.data.frame(group, id_group, correlative_group) As for your last question: > PD: Last question. Is there any way to save in a variable the internal that > R uses (those numbers that appear on left side of a dataframe)? You mean the row names? See ?row.names Sarah 2011/2/16 Sebastián Daza <sebastian.d...@gmail.com>: > Hello everyone, > I am new in R and I am trying to create a id number (a correlative sequence > of numbers) by group, and a correlative sequence of numbers inside each > group (my idea is to get statistics by group without having to aggregate > the database). Here an example: > > group id_group correlative_group > A 1 1 > A 1 2 > A 1 3 > A 1 4 > B 2 1 > B 2 2 > B 2 3 > C 3 1 > C 3 2 > C 3 3 > C 3 4 > C 3 5 > > Unfortunately, I have been able to find an explicit lag function to get > id_group (I know I can get it using aggregate and merge but I'm just > wondering if there is another way to do it). With regard to the > correlative_group, I don't have any clue about how to do it. > > PD: Last question. Is there any way to save in a variable the internal that > R uses (those numbers that appear on left side of a dataframe)? > > Thank you, > Sebastian. > -- > Sebastián Daza > sebastian.d...@gmail.com > -- Sarah Goslee http://www.functionaldiversity.org ______________________________________________ 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.