[Rd] Should numeric()/character() etc initialize with NA instead of 0 or ""?

From: Fabian Scheipl <Fabian.Scheipl_at_stat.uni-muenchen.de>
Date: Mon, 5 Nov 2007 16:16:53 +0100


Wouldn't it make programming more error-resistant if vectors were initialized with missing data, instad of zeroes or ""?

That way, if you assign values to a vector elementwise and you miss some elements
(because their indices were not selected or because the assignment didn't
work out, see below for code examples)
this would be immediately obvious from the value of the vector elements themselves
and programming errors would be far less easy to overlook.

e.g.

x <- numeric(n) or
for( i in seq(along = x) )
{

      try(x[i] <- function.which.might.crash( args[i] )) }

or

x <- numeric(n)
x[condition1] <- foo(args1)
x[condition2] <- foo(args2)
...
x[conditionN] <- foo(argsN)

will produce x without any NAs even if function.which.might.crash() actually did crash during the loop or
if there are indices for which none of conditions 1 to N were true and you cannot distinguish between zeroes which
are real results and zeroes that remained unchanged since initialization of the vector.

In a sense, initializing with NAs would also be more consistent with vector(n, mode = "list"), which produces a list of n NULL-objects.
(numeric(10) is just a wrapper for vector(10, mode="numeric"))

Let me know what you think.

Regards,
Fabian

        [[alternative HTML version deleted]]



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Mon 05 Nov 2007 - 17:22:51 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 05 Nov 2007 - 21:30:15 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.