Thanks! That solved the problem.
Dimitris Rizopoulos-4 wrote: > > try this: > > x <- factor(c('a','a','a','a','a','b','b','b','c','d','d','d')) > y <- c(1,3,6,8,8,3,4,7,5,6,7,10) > X <- data.frame(x, y) > > cbind(X, z = ave(X$y, X$x, > FUN = function (x) match(x, unique(x)) - 1)) > > > I hope it helps. > > Best, > Dimitris > > > On 2/17/2011 11:15 AM, mathijsdevaan wrote: >> >>> Here is a method that works despite generating a warning: >>> cbind(X, z = ave(X$y, X$x, FUN = seq) - 1) >>> >>> David Winsemius, MD >>> West Hartford, CT >> >> I was happy a bit too early. There's still an error: >> >> x<-as.factor(c('a','a','a','a','a','b','b','b','c','d','d','d')) >> y<-c(1,3,6,8,8,3,4,7,5,6,7,10) >> X<-data.frame(x,y) >> X >> x y >> 1 a 1 >> 2 a 3 >> 3 a 6 >> 4 a 8 >> 5 a 8 >> 6 b 3 >> 7 b 4 >> 8 b 7 >> 9 c 5 >> 10 d 6 >> 11 d 7 >> 12 d 10 >> >> cbind(X, z = ave(X$y, X$x, FUN = seq) - 1) >> >> x y z >> 1 a 1 0 >> 2 a 3 1 >> 3 a 6 2 >> 4 a 8 3 >> 5 a 8 4 >> 6 b 3 0 >> 7 b 4 1 >> 8 b 7 2 >> 9 c 5 0 >> 10 d 6 0 >> 11 d 7 1 >> 12 d 10 2 >> >> Using this function provides a value of 4 in row 5, column c, while it >> should be 3. Is there another way I can solve my problem? Thanks! >> >> M > > -- > Dimitris Rizopoulos > Assistant Professor > Department of Biostatistics > Erasmus University Medical Center > > Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands > Tel: +31/(0)10/7043478 > Fax: +31/(0)10/7043014 > Web: http://www.erasmusmc.nl/biostatistiek/ > > ______________________________________________ > 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. > > -- View this message in context: http://r.789695.n4.nabble.com/Count-factor-if-tp3306856p3310714.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.