何时使用缺失值与空值来传递R中未定义的函数参数 [英] When to using missing versus NULL values for passing undefined function arguments in R
问题描述
在编写R函数时,我已经将未定义的参数
作为NULL值传递,然后测试它们是否为NULL,即
fun < - function(x = NULL){
if(is.null(x))
...
}
然而,我最近发现将未定义的参数作为缺失传递的可能性,即
fun < - function(x){
if(missing(x))
...
}
R文件指出
目前只能使用缺失在
的直接主体中定义参数的函数,不在
嵌套函数或本地调用的主体中。这可能会在未来发生变化。
显然,这是使用缺少值来确定未定义值的一个缺点,有没有其他人或意识到?或者用更有用的形式来描述问题什么时候使用缺失值和空值来传递R中未定义的函数参数?为什么?
解决方案 (x)似乎比使用默认参数< NULL
。 > require('microbenchmark')
> f1< - 函数(x = NULL)is.null(x)
> f2< - 函数(x)缺失(x)
> microbenchmark(f1(1),f2(1))
单位:纳秒
expr min lq中值uq max neval
f1(1)615 631 647.5 800.5 3024 100
f2 1)497 511 567.0 755.5 7916 100
>微单位标记(f1(),f2())
单位:纳秒
expr分钟lq中位数uq max neval
f1()589 619 627 745.5 3561 100
f2()437 448 463 479.0 2869 100
请注意,在 f1
如果您拨打电话 f1()
,则 f1
中读取。
第二种情况比第一种更普遍。 missing()
表示用户没有传递任何值。 is.null()
(with NULL
default arg)表明用户要么没有传递任何东西,要么他/她通过 NULL
。
顺便说一句, plot.default()
和 chisq.test()
使用 NULL
作为第二个参数。另一方面,对于<$ c $ getS3method('t.test','default')
使用 NULL
c> y 参数和 missing()
用于 mu
(为了做好准备很多使用场景)。
我认为一些R用户会喜欢 f1
类型的函数,特别是在处理 * apply
sapply(list(1,NULL, 2,NULL),f1)
实现 f2
情况并非如此简单。
To date when writing R functions I've passed undefined arguments as NULL values and then tested whether they are NULL i.e.
fun <- function (x = NULL) {
if(is.null(x))
...
}
However I recently discovered the possibility of passing undefined arguments as missing i.e.
fun <- function (x) {
if(missing(x))
...
}
The R documentation states that
Currently missing can only be used in the immediate body of the function that defines the argument, not in the body of a nested function or a local call. This may change in the future.
Clearly this is one disadvantage of using missing to determine undefined values are there any others people or aware of? Or to phrase the question in a more useful form "When do you use missing versus NULL values for passing undefined function arguments in R and why?"
missing(x)
seems to be a bit faster than using default arg to x
equal to NULL
.
> require('microbenchmark')
> f1 <- function(x=NULL) is.null(x)
> f2 <- function(x) missing(x)
> microbenchmark(f1(1), f2(1))
Unit: nanoseconds
expr min lq median uq max neval
f1(1) 615 631 647.5 800.5 3024 100
f2(1) 497 511 567.0 755.5 7916 100
> microbenchmark(f1(), f2())
Unit: nanoseconds
expr min lq median uq max neval
f1() 589 619 627 745.5 3561 100
f2() 437 448 463 479.0 2869 100
Note that in the f1
case x
is still reported as missing if you make a call f1()
, but it has a value that may be read within f1
.
The second case is more general than the first one. missing()
just means that the user did not pass any value. is.null()
(with NULL
default arg) states that the user either did not pass anything or he/she passed NULL
.
By the way, plot.default()
and chisq.test()
use NULL
for their second arguments. On the other hand, getS3method('t.test', 'default')
uses NULL
for y
argument and missing()
for mu
(in order to be prepared for many usage scenarios).
I think that some R users will prefer f1
-type functions, especially when working with the *apply
family:
sapply(list(1, NULL, 2, NULL), f1)
Achieving that in the f2
case is not so straightforward.
这篇关于何时使用缺失值与空值来传递R中未定义的函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!