Mean doesn't work either... I understand that the message "replacement has 0
items, need 37597770" implies that the function is not returning any values,
but I don't understand why then this is not the case in the example.

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),]

#first option - works fine in example and my target data frame
DF$F = ave(DF$D,DF$B, FUN = function(x) cumsum(x)-x) 
DF$G = ave(DF$E,DF$B, FUN = function(x) cumsum(x)-x)

#second option - works fine in example but not in my target data frame
foo <- function(x) 
     { 
     unlist(lapply(x, FUN = function(z) cumsum(z) - z)) 
     }
n<-ave(DF[,c(4:5)],DF$B,FUN = foo)

Why is this second option not working in my target data frame (which is much
bigger than the example)?
Thanks



-- 
View this message in context: 
http://r.789695.n4.nabble.com/Error-tp3324531p3325857.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.

Reply via email to