[R] NULL or NA for missing function arguments?
Duncan Murdoch
murdoch at stats.uwo.ca
Mon Oct 16 23:25:47 CEST 2006
On 10/16/2006 5:06 PM, Brahm, David wrote:
> Hans-Peter <gchappi at gmail.com> wrote:
>> I am troubled by the use of NULL or NA to indicate
>> missing/non-specified function arguments.
>
> I suggest using NULL for arguments which are vectors or lists of
> unspecified length, and NA for "scalars" (arguments whose length
> should always be one, such as na.rm). I rarely use missing arguments,
> as they are harder to pass down to other functions.
To be a little more precise: they are easy to pass down, but hard to do
anything else with. For example:
> f <- function(x) if (missing(x)) print("missing!") else print(x)
> g <- function(x) f(x)
> h <- function(x) { y <- x; f(y) }
> g()
[1] "missing!"
> h()
Error in h() : argument "x" is missing, with no default
Duncan Murdoch
More information about the R-help
mailing list