Here's one possible approach. It assumes that a$name is a factor, as it is in your example, but does not require that each sequence has a unique value (see second example).
> a <- data.frame(name = c(rep("A", 3), rep("B", 5), rep("C", 10))) > data.frame(name=a, val=unlist(sapply(rle(as.numeric(a[,1]))$lengths, > function(x)seq(1, x, by=1)))) name val 1 A 1 2 A 2 3 A 3 4 B 1 5 B 2 6 B 3 7 B 4 8 B 5 9 C 1 10 C 2 11 C 3 12 C 4 13 C 5 14 C 6 15 C 7 16 C 8 17 C 9 18 C 10 > b <- data.frame(name = c(rep("A", 3), rep("B", 5), rep("C", 10), rep("B", 3))) > data.frame(name=b, val=unlist(sapply(rle(as.numeric(b[,1]))$lengths, > function(x)seq(1, x, by=1)))) name val 1 A 1 2 A 2 3 A 3 4 B 1 5 B 2 6 B 3 7 B 4 8 B 5 9 C 1 10 C 2 11 C 3 12 C 4 13 C 5 14 C 6 15 C 7 16 C 8 17 C 9 18 C 10 19 B 1 20 B 2 21 B 3 Sarah On Tue, Mar 6, 2012 at 11:47 AM, syrvn <ment...@gmx.net> wrote: > Hi, > > Is there a simple way of doing the following in R? > > > a <- data.frame(name = c(rep("A", 3), rep("B", 5), rep("C", 10))) > >> a > name > 1 A > 2 A > 3 A > 4 B > 5 B > 6 B > 7 B > 8 B > 9 C > 10 C > 11 C > 12 C > 13 C > 14 C > 15 C > 16 C > 17 C > 18 C > > Do some fancy R code here: > >> a > name val > 1 A 1 > 2 A 2 > 3 A 3 > 4 B 1 > 5 B 2 > 6 B 3 > 7 B 4 > 8 B 5 > 9 C 1 > 10 C 2 > 11 C 3 > 12 C 4 > 13 C 5 > 14 C 6 > 15 C 7 > 16 C 8 > 17 C 9 > 18 C 10 > > -- 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.