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

2007-11-05 Thread Fabian Scheipl
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@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] Wishlist: fix error in documentation for body<- (PR#13318)

2008-11-21 Thread fabian . scheipl
Full_Name: Fabian Scheipl
Version: 2.8.0
OS: Windows, Linux
Submission from: (NULL) (138.246.7.150)


It seems to me that the documentation for body<- is wrong.
The help file for  

body(fun, envir = environment(fun)) <- value

says that:

value can be an expression or a list of R expressions.

This produces errors however:



> f <- function(x){}
> body(f) <- list(expression(res <- x^2),expression(return(x)))
Fehler in as.function.default(c(formals(fun), value), envir) : 
  ungültiges formales Argument für "function"




The only way to assign multiple statements to a function body
that I could get to work is to put all the statements into a single expression,
(separated by ';' and in curly brackets):



body(f) <- expression({res <- x^2; return(x)})



I think the words 'or a list of R expressions.' should be removed.

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel