简体   繁体   English

R中的NULL和字符(0)有什么区别?

[英]What is the difference between NULL and character(0) in R?

What is the difference between NULL and character(0) | NULL和字符(0)|之间有什么区别 integer(0) etc? 整数(0)等?

> identical(NULL, character(0))
[1] FALSE

> is.null(integer(0))
[1] FALSE

> str(character(0))
 chr(0) 

> str(NULL)
 NULL

In general it seems you can pass NULL as parameters into functions, and that an empty vector is generally returned as character(0) , integer(0) , etc. 通常,您似乎可以将NULL作为参数传递给函数,并且通常将空向量作为character(0)integer(0)等返回。

Why is this the case? 为什么会这样? Come to think of it, is there a test for zero-ness, a la is.integer0 ? 想一想,是否有零测试, la is.integer0

The R Language Definitio n has this on NULL : R语言定义 n具有NULL

There is a special object called NULL. 有一个名为NULL的特殊对象。 It is used whenever there is a need to indicate or specify that an object is absent. 只要需要指示或指定对象不存在,就会使用它。 It should not be confused with a vector or list of zero length. 它不应与矢量或零长度列表混淆。 The NULL object has no type and no modifiable properties. NULL对象没有类型,也没有可修改的属性。 There is only one NULL object in R, to which all instances refer. R中只有一个NULL对象,所有实例都引用该对象。 To test for NULL use is.null. 要测试NULL,请使用is.null。 You cannot set attributes on NULL. 您不能在NULL上设置属性。

So by definition NULL is very different to zero length vectors. 因此,根据定义, NULL与零长度向量非常不同。 A zero length vector very much isn't absent. 存在零长度矢量。 NULL is really a catch-all for something that is absent or not set, but not missing-ness, which is the job of NA . 对于没有或没有设置的东西, NULL实际上是一个全能,但不是缺失,这是NA的工作。 There is an exception, the zero-length pairlist, as mentioned by @Owen. 正如@Owen所提到的,有一个例外,零长度的旋风。 The Language Definition states: 语言定义指出:

A zero-length pairlist is NULL, as would be expected in Lisp but in contrast to a zero-length list. 零长度的pairlist是NULL,正如在Lisp中所期望的那样,但与零长度列表相反。

which highlights the exception in this case. 这突出了这种情况下的例外情况。

To test for a zero-length vector use something like if(length(foo) == 0L) for example. 要测试零长度向量,请使用if(length(foo) == 0L)的东西。 And combine that with a class check ( is.character(foo) ) if you want a specific type of zero length vector. 如果你想要一个特定类型的零长度向量,并将它与类检查( is.character(foo) )结合起来。

The other guys have the right answers, but I want to add a few curiosities. 其他人有正确的答案,但我想添加一些好奇心。

First, it's not quite true that NULL "is used whenever there is a need to indicate or specify that an object is absent" as it says in the doc. 首先,正如文档中所述,当需要指示或指定某个对象不存在时,使用NULL“并不完全正确”。 There are actually 2 other "no data" values in R (not counting NA, which is not a complete value). R中实际上有2个其他“无数据”值(不计算NA,这不是一个完整的值)。

There's "missing", which is used for missing arguments: 有“缺失”,用于缺少参数:

alist(x=)$x ALIST(X =)$ X

> identical(NULL, alist(x=)$x)
[1] FALSE
> y = alist(x=)$x
> y
Error: argument "y" is missing, with no default

Then there's "unbound", which you can't (AFAIK) access directly, but using C: 然后是“未绑定”,你不能直接访问(AFAIK),但使用C:

SEXP getUnbound(void) {
    return R_UnboundValue;
}

> x = .Call("getUnbound")
> x
Error: object 'x' not found

Here's a partial answer, beginning by simply quoting the R Language Definition Guide: 这是一个部分答案,首先简单引用R语言定义指南:

There is a special object called NULL. 有一个名为NULL的特殊对象。 It is used whenever there is a need to indicate or specify that an object is absent. 只要需要指示或指定对象不存在,就会使用它。 It should not be confused with a vector or list of zero length. 它不应与矢量或零长度列表混淆。 The NULL object has no type and no modifiable properties. NULL对象没有类型,也没有可修改的属性。 There is only one NULL object in R, to which all instances refer. R中只有一个NULL对象,所有实例都引用该对象。 To test for NULL use is.null. 要测试NULL,请使用is.null。 You cannot set attributes on NULL. 您不能在NULL上设置属性。

I take that to mean that zero length vectors can have attributes, whereas NULL cannot: 我认为这意味着零长度向量可以具有属性,而NULL不能:

> x <- character(0)
> y <- NULL
> attr(x,"name") <- "nm"
> attr(y,"name") <- "nm"
Error in attr(y, "name") <- "nm" : attempt to set an attribute on NULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM