Hello, I have a data frame of the following form block trial dv 1 1 10 1 1 11 1 1 9 1 2 12 1 2 9 1 3 12 1 3 13 1 3 14 1 3 15 1 4 12 1 4 11 1 4 10 1 4 9 1 4 11 and need to number the rows consecutively in the following manner block trial dv sequ 1 1 10 1 1 1 11 2 1 1 9 3 1 2 12 1 1 2 9 2 1 3 12 1 1 3 13 2 1 3 14 3 1 3 15 4 1 4 12 1 1 4 11 2 1 4 10 3 1 4 9 4 1 4 11 5 Any idea how to solve this problem? I tried but did not succeed :-( Best Jens B?lte
Henrique Dallazuanna
2009-Jul-10 13:33 UTC
[R] How to number a subset consecutively from 1 to n?
Try this: transform(d, sequ = ave(seq(trial), trial, FUN=seq)) On Fri, Jul 10, 2009 at 10:27 AM, Jens Bölte <boelte@psy.uni-muenster.de>wrote:> Hello, > > I have a data frame of the following form > > block trial dv > 1 1 10 > 1 1 11 > 1 1 9 > 1 2 12 > 1 2 9 > 1 3 12 > 1 3 13 > 1 3 14 > 1 3 15 > 1 4 12 > 1 4 11 > 1 4 10 > 1 4 9 > 1 4 11 > > and need to number the rows consecutively in the following manner > > block trial dv sequ > 1 1 10 1 > 1 1 11 2 > 1 1 9 3 > 1 2 12 1 > 1 2 9 2 > 1 3 12 1 > 1 3 13 2 > 1 3 14 3 > 1 3 15 4 > 1 4 12 1 > 1 4 11 2 > 1 4 10 3 > 1 4 9 4 > 1 4 11 5 > > Any idea how to solve this problem? I tried but did not succeed :-( > > Best Jens Bölte > > ______________________________________________ > 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. > >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Jorge Ivan Velez
2009-Jul-10 13:40 UTC
[R] How to number a subset consecutively from 1 to n?
Dear Jens, Here is one way: x$sequ <- ave(x$dv, paste(x$block, x$trial, sep=""), FUN = function(x) seq(length(x)) ) x where x is your data. HTH, Jorge On Fri, Jul 10, 2009 at 9:27 AM, Jens Bölte <boelte@psy.uni-muenster.de>wrote:> Hello, > > I have a data frame of the following form > > block trial dv > 1 1 10 > 1 1 11 > 1 1 9 > 1 2 12 > 1 2 9 > 1 3 12 > 1 3 13 > 1 3 14 > 1 3 15 > 1 4 12 > 1 4 11 > 1 4 10 > 1 4 9 > 1 4 11 > > and need to number the rows consecutively in the following manner > > block trial dv sequ > 1 1 10 1 > 1 1 11 2 > 1 1 9 3 > 1 2 12 1 > 1 2 9 2 > 1 3 12 1 > 1 3 13 2 > 1 3 14 3 > 1 3 15 4 > 1 4 12 1 > 1 4 11 2 > 1 4 10 3 > 1 4 9 4 > 1 4 11 5 > > Any idea how to solve this problem? I tried but did not succeed :-( > > Best Jens Bölte > > ______________________________________________ > 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. > >[[alternative HTML version deleted]]