[R] Error
William Dunlap
wdunlap at tibco.com
Mon Feb 28 19:58:05 CET 2011
You can debug this sort of thing yourself by altering
your function to tell you what its inputs are when
things go wrong. E.g., change your original
foo <- function(x) {
unlist(lapply(x, FUN = function(z) cumsum(z) - z))
}
to
foo <- function(x) {
retval <- unlist(lapply(x, FUN=function(z), cumsum(z) - z))
if (length(retval)==0)) {
cat("foo: length(retval)==0 when x=\n")
str(x)
stop("problem in foo")
}
retval
}
For a more general solution, try setting options("error") to one of
the debugging error handlers. options(error=recover)
works but there are others. It takes a little while
to figure out how to use the error handler but once
you do you can solve your own problems.
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of
> rex.dwyer at syngenta.com
> Sent: Monday, February 28, 2011 9:31 AM
> To: murdoch.duncan at gmail.com; mathijsdevaan at gmail.com
> Cc: r-help at r-project.org
> Subject: Re: [R] Error
>
> I have to agree that it's pretty hard to take something that
> works and figure out why it doesn't work :)
> The only other suggestion is that sometimes I find that this
> sort of error goes away if I add "drop=FALSE" to the
> subsetting, and, if so, that usually lets me figure out why.
>
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Duncan Murdoch
> Sent: Sunday, February 27, 2011 5:52 AM
> To: mathijsdevaan
> Cc: r-help at r-project.org
> Subject: Re: [R] Error
>
> 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
>
> ______________________________________________
> R-help at 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.
>
>
>
>
> message may contain confidential information. If you are not
> the designated recipient, please notify the sender
> immediately, and delete the original and any copies. Any use
> of the message by you is prohibited.
> ______________________________________________
> R-help at 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.
>
More information about the R-help
mailing list