[R] Count factor if

mathijsdevaan mathijsdevaan at gmail.com
Tue Feb 15 15:33:06 CET 2011


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.



More information about the R-help mailing list