[R] Error

Duncan Murdoch murdoch.duncan at gmail.com
Sun Feb 27 11:52:06 CET 2011


On 11-02-26 8:26 AM, mathijsdevaan wrote:
> 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)?

Presumably something is different about it.  I don't see how you expect 
people to debug your problem when you don't show it.

If you want help, you need to give us an example that shows the error. 
Start with your large dataset, and shrink it as much as possible, but 
not so much that the error goes away.  (I suspect when you do this, 
you'll end up seeing your error yourself.  But maybe not.)

Duncan Murdoch



More information about the R-help mailing list