[R] Potential Error/Bug?: addition of NULL during arithmetic
Prof Brian Ripley
ripley at stats.ox.ac.uk
Thu Aug 28 15:25:01 CEST 2008
On Tue, 26 Aug 2008, Eric DeWitt wrote:
> I encountered an error that does not make sense to me given my reading
> of the documentation and does not appear to be referenced in the list
> archives or online. The error occurred when a function received a NULL
> value rather than a numeric value as a parameter and then attempted to
> use the NULL value in the calculateion. The error: "Error during wrapup:
> nothing to replace with" can be easily recreated with the following code:
>
>> 1 + NULL # note that the opperation succeeds and returns a numeric vector with dim(NULL)
> numeric(0)
As documented, of course.
>> bar <- 1
>> bar
> [1] 1
>> foo <- 1 + NULL
>> foo
> numeric(0)
>
>> bar <- bar + foo # note that here the assignment operation succeeds and 1 + (1 + NULL) -> numeric(0)
>> bar
> numeric(0)
>
>> bar <- c(1, 1) # however if the assignment is into a vector
>> bar[1] <- bar[1] + foo # note that the mathematical operation is identical, but the assignment fails
> Error during wrapup: nothing to replace with
That's the trouble, this is not the same operation at all. This is
replacing one element of a vector, not naming the result 'bar'.
> If this is the intended behavior, a more informative error message (e.g.
> 'attempt to assign NULL to vector element') would be useful. If it is
> not the intended behavior, should I log this as a bug?
It is the intended and documented behaviour. You tried to set element 1 of
bar to bar[1] + foo, which is numeric(0) (by the documented recycling
rules). The error message: 'nothing to replace with' seems very
informative to me. If you put some effort into understanding your own
errors by doing things step by step, as in
> bar <- c(1, 1)
> foo <- bar[1] + foo
> bar[1] <- foo
Error in bar[1] <- foo : nothing to replace with
> foo
numeric(0)
you might learn more.
I don't know how you got an 'Error during wrapup': that is not
reproducible (it might be a function of using a console, if you did).
>
> -eric
>
>> sessionInfo()
> R version 2.7.1 (2008-06-23)
> powerpc-apple-darwin8.10.1
>
> locale:
> en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8
>
> attached base packages:
> [1] stats graphics grDevices utils datasets methods base
>
> ______________________________________________
> 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.
>
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list