On Tue, Mar 06, 2012 at 09:32:37AM -0800, syrvn wrote: > Hi Sarah, > > thanks a lot for this peace of code. > > Is it possible to give the second sequence of "B" in your second example > (data frame b) > the numbers 6, 7 and 8 instead of 1, 2 and 3 again? In my real data I have > more columns than > only those two and sometimes the are sorted differently so that column name > rather looks like > your second example (data frame b) rather than your first one.
Hi. Try the following. dat <- data.frame(name = rep(c("A", "B", "C", "A", "B", "C"), times=c(2, 3, 4, 3, 4, 5))) num <- ave(1:nrow(dat), dat$name, FUN=function(x) { seq.int(along=x) }) cbind(dat, num) name num 1 A 1 2 A 2 3 B 1 4 B 2 5 B 3 6 C 1 7 C 2 8 C 3 9 C 4 10 A 3 11 A 4 12 A 5 13 B 4 14 B 5 15 B 6 16 B 7 17 C 5 18 C 6 19 C 7 20 C 8 21 C 9 Hope this helps. Petr Savicky. ______________________________________________ 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.