Hi. Here is one approach: x <- rep(c("A", "B", "C"), c(3,1,2)) DF <- data.frame(x, stringsAsFactors=FALSE) cbind(DF, new_c=c(lapply(rle(DF$x)[[1]], function(x) 1:x), recursive=T))
Andrija On Wed, May 28, 2014 at 10:46 PM, Marcos Santos <mmsantos...@gmail.com>wrote: > I have a "data frame" like this: > > Category Observed_value A 100 A 130 A 140 B 90 C 80 D 120 D 130 > > I need to create an index column that show the number of the observation > for each category. I have three observations in the category A, one in > category B, one in category C and two in category D. The new matrix should > be like this: > > > Category Observed_value Index A 100 1 A 130 2 A 140 3 B 90 1 C 80 1 > D 120 1 D 130 2 > > Please, could anyone help me with this problem? > > Thank you. > -- > Marcos Martins Santos > mmsantos...@gmail.com > -- > Skype: marcosmartinssantos > > [[alternative HTML version deleted]] > > ______________________________________________ > 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]] ______________________________________________ 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.