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)))> aname 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:> aname 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 -- View this message in context: http://r.789695.n4.nabble.com/Add-number-series-to-data-frame-tp4450495p4450495.html Sent from the R help mailing list archive at Nabble.com.
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 <mentor_ at 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
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. Regards -- View this message in context: http://r.789695.n4.nabble.com/Add-number-series-to-data-frame-tp4450495p4450656.html Sent from the R help mailing list archive at Nabble.com.
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.
Hi Petr, that works great. thanks! I was working my head off on that :) -- View this message in context: http://r.789695.n4.nabble.com/Add-number-series-to-data-frame-tp4450495p4452962.html Sent from the R help mailing list archive at Nabble.com.