Try this:

cbind(X, z = ave(X$y, X$x, FUN = order) - 1)


On Tue, Feb 15, 2011 at 12:33 PM, mathijsdevaan <mathijsdevaan@gmail.com>wrote:

>
> Hi,
>
> I have a data frame containing two columns:
>
> x<-as.factor(c('a','a','a','a','a','b','b','b','c','d','d','d'))
> y<-c(1,3,6,8,12,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 12
> 6  b  3
> 7  b  4
> 8  b  7
> 9  c  5
> 10 d  6
> 11 d  7
> 12 d 10
>
> I would like to add a column that counts the number of occurences of x for
> all y < focal y. So,
>
> Xnew
>   x  y  z
> 1  a  1  0
> 2  a  12  4
> 3  a  6  2
> 4  a  8  3
> 5  a 3  1
> 6  b  3  1
> 7  b  4  2
> 8  b  7  3
> 9  c  5  1
> 10 d  6  1
> 11 d  7  2
> 12 d 10  3
>
> I guess this should be easy, but I have no clue on how to do it. Thanks
> very
> much!
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Count-factor-if-tp3306856p3306856.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.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

	[[alternative HTML version deleted]]

