[R] conditional seq

mathijsdevaan mathijsdevaan at gmail.com
Thu Feb 24 15:13:07 CET 2011


Problem solved:

DF = data.frame(read.table(textConnection("    A  B  C  D  E 
1 1  a  1999  1  0 
2 1  b  1999  0  1 
3 1  c  1999  0  1 
4 1  d  1999  1  0 
5 2  c  2001  1  0 
6 2  d  2001  0  1 
7 3  a  2004  0  1 
8 3  b  2004  0  1 
9 3  d  2004  0  1 
10 4  b  2001  1  0 
11 4  c  2001  1  0 
12 4  d  2001  0  1"),head=TRUE,stringsAsFactors=FALSE)) 
DF = DF[order(DF$B,DF$C),] 
foo1<- function(x) 
	{ 
  	unlist(lapply(x, FUN = function(z) cumsum(z) - z)) 
	}
DF<-cbind(DF[,c(1:3)],ave(DF[, c("D", "E")],DF$B, FUN = foo))
DF$F<-paste(DF$B,"a",DF$C,sep="")
foo2<- function(x) 
	{ 
  	lapply(x, FUN = function(z) min(z))
	}
DF<-cbind(DF[,c(1:3)],ave(DF[, c("D", "E")],DF$F,FUN = foo2))

Thanks,

Mathijs
-- 
View this message in context: http://r.789695.n4.nabble.com/conditional-seq-tp3321327p3322678.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list