[R] Count factor if

mathijsdevaan mathijsdevaan at gmail.com
Thu Feb 17 11:15:28 CET 2011


>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
-- 
View this message in context: http://r.789695.n4.nabble.com/Count-factor-if-tp3306856p3310482.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list