何时在R中使用missing和NULL值传递未定义的函数参数,为什么? [英] When to use missing versus NULL values for passing undefined function arguments in R, and why?

查看:115
本文介绍了何时在R中使用missing和NULL值传递未定义的函数参数,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,在编写R函数时,我已经传递了未定义的参数 为NULL值,然后测试它们是否为NULL,即

To date when writing R functions I've passed undefined arguments as NULL values and then tested whether they are NULL i.e.

f1 <- function (x = NULL) {
   if(is.null(x))
      ...
}

但是我最近发现有可能将未定义的参数传递为丢失,即

However I recently discovered the possibility of passing undefined arguments as missing i.e.

f2 <- function (x) {
   if(missing(x))
      ...
}

R文档指出

当前缺少的内容只能用于以下内容的直接正文中: 定义参数的函数,而不是在主体中 嵌套函数或本地调用.将来可能会改变.

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.

很明显,这是使用missing确定未定义值的一个缺点,是否还有其他人或您知道吗?或者以更有用的形式表达问题的意思:何时在R中使用missing和NULL值传递未定义的函数参数,为什么?"

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)似乎比对x等于NULL的默认arg要快一些.

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

请注意,在f1情况下,如果您拨打f1(),仍会报告x丢失,但是它的值可以在f1中读取.

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.

第二种情况比第一种情况更笼统. missing()仅表示用户未传递任何值. is.null()(带有NULL默认arg)指出用户未通过任何操作或通过了NULL.

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.

顺便说一句,plot.default()chisq.test()使用NULL作为第二个参数.另一方面,getS3method('t.test', 'default')使用NULL表示y参数,使用missing()表示mu(以便为许多使用情况做好准备).

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).

我认为某些R用户会更喜欢f1类型的函数,尤其是在使用*apply系列时:

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)

f2情况下实现起来并非如此简单.

Achieving that in the f2 case is not so straightforward.

这篇关于何时在R中使用missing和NULL值传递未定义的函数参数,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆