在R中,NULL和字符(0)的区别是什么?

时间:2021-05-12 13:20:25

What is the difference between NULL and character(0) | integer(0) etc?

NULL和字符(0)|整数(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作为参数传递到函数中,而空的向量通常作为字符(0)、整数(0)等返回。

Why is this the case? Come to think of it, is there a test for zero-ness, a la is.integer0?

为什么会这样?想一下,是否有对零度的测试,一个la。integer0?

3 个解决方案

#1


31  

The R Language Definition has this on NULL:

R语言的定义是这样的:

There is a special object called 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. There is only one NULL object in R, to which all instances refer. To test for NULL use is.null. You cannot set attributes on NULL.

有一个特殊的对象叫做空。每当需要指示或指定对象不在时,就使用它。它不应该与矢量或零长度的列表混淆。NULL对象没有类型,也没有可修改的属性。在R中只有一个空对象,所有实例都引用它。要测试空值,请使用is.null。不能将属性设置为空。

So by definition NULL is very different to zero length vectors. 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. There is an exception, the zero-length pairlist, as mentioned by @Owen. The Language Definition states:

所以根据定义,NULL是非常不同于零长度的向量。一个零长度的向量是不存在的。NULL实际上是一种不存在或没有设置的东西,而不是不正确的,这是NA的工作。有一个例外,零长度的pairlist,如@Owen所提到的。语言的定义:

A zero-length pairlist is NULL, as would be expected in Lisp but in contrast to a zero-length list.

零长度的pairlist是空的,正如在Lisp中所期望的那样,但与零长度的列表相反。

which highlights the exception in this case.

这在本例中突出了异常。

To test for a zero-length vector use something like if(length(foo) == 0L) for example. And combine that with a class check (is.character(foo)) if you want a specific type of zero length vector.

例如,要测试一个零长度的向量,就使用if(length(foo) == 0L)。如果您想要一个特定类型的零长度向量,那么将其与类检查(is.character(foo))结合起来。

#2


7  

Here's a partial answer, beginning by simply quoting the R Language Definition Guide:

这里有一个部分答案,从简单引用R语言定义指南开始:

There is a special object called 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. There is only one NULL object in R, to which all instances refer. To test for NULL use is.null. You cannot set attributes on NULL.

有一个特殊的对象叫做空。每当需要指示或指定对象不在时,就使用它。它不应该与矢量或零长度的列表混淆。NULL对象没有类型,也没有可修改的属性。在R中只有一个空对象,所有实例都引用它。要测试空值,请使用is.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

#3


7  

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. There are actually 2 other "no data" values in R (not counting NA, which is not a complete value).

首先,在文档中说“当有需要指出或指定对象不存在时,就使用NULL”,这不是完全正确的。在R中实际上还有另外两个“没有数据”值(不包括NA,这不是一个完整的值)。

There's "missing", which is used for missing arguments:

这里有“missing”,用于缺少参数:

alist(x=)$x

船向一边倾斜的$ 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:

然后是“unbound”,你不能直接访问,但是使用C:

SEXP getUnbound(void) {
    return R_UnboundValue;
}

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

#1


31  

The R Language Definition has this on NULL:

R语言的定义是这样的:

There is a special object called 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. There is only one NULL object in R, to which all instances refer. To test for NULL use is.null. You cannot set attributes on NULL.

有一个特殊的对象叫做空。每当需要指示或指定对象不在时,就使用它。它不应该与矢量或零长度的列表混淆。NULL对象没有类型,也没有可修改的属性。在R中只有一个空对象,所有实例都引用它。要测试空值,请使用is.null。不能将属性设置为空。

So by definition NULL is very different to zero length vectors. 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. There is an exception, the zero-length pairlist, as mentioned by @Owen. The Language Definition states:

所以根据定义,NULL是非常不同于零长度的向量。一个零长度的向量是不存在的。NULL实际上是一种不存在或没有设置的东西,而不是不正确的,这是NA的工作。有一个例外,零长度的pairlist,如@Owen所提到的。语言的定义:

A zero-length pairlist is NULL, as would be expected in Lisp but in contrast to a zero-length list.

零长度的pairlist是空的,正如在Lisp中所期望的那样,但与零长度的列表相反。

which highlights the exception in this case.

这在本例中突出了异常。

To test for a zero-length vector use something like if(length(foo) == 0L) for example. And combine that with a class check (is.character(foo)) if you want a specific type of zero length vector.

例如,要测试一个零长度的向量,就使用if(length(foo) == 0L)。如果您想要一个特定类型的零长度向量,那么将其与类检查(is.character(foo))结合起来。

#2


7  

Here's a partial answer, beginning by simply quoting the R Language Definition Guide:

这里有一个部分答案,从简单引用R语言定义指南开始:

There is a special object called 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. There is only one NULL object in R, to which all instances refer. To test for NULL use is.null. You cannot set attributes on NULL.

有一个特殊的对象叫做空。每当需要指示或指定对象不在时,就使用它。它不应该与矢量或零长度的列表混淆。NULL对象没有类型,也没有可修改的属性。在R中只有一个空对象,所有实例都引用它。要测试空值,请使用is.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

#3


7  

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. There are actually 2 other "no data" values in R (not counting NA, which is not a complete value).

首先,在文档中说“当有需要指出或指定对象不存在时,就使用NULL”,这不是完全正确的。在R中实际上还有另外两个“没有数据”值(不包括NA,这不是一个完整的值)。

There's "missing", which is used for missing arguments:

这里有“missing”,用于缺少参数:

alist(x=)$x

船向一边倾斜的$ 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:

然后是“unbound”,你不能直接访问,但是使用C:

SEXP getUnbound(void) {
    return R_UnboundValue;
}

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