R中NULL和character(0)有什么区别?

发布于 2024-12-01 13:49:01 字数 387 浏览 0 评论 0原文

NULL 和字符(0) 有什么区别 |整数(0)等?

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

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

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

> str(NULL)
 NULL

一般来说,您似乎可以将 NULL 作为参数传递给函数,并且空向量通常以 character(0)integer(0) 形式返回code>等等

为什么会这样呢?想一想,是否有一个零度测试,a la is.integer0

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情未る 2024-12-08 13:49:01

R 语言定义n 在 NULL< 上有此内容/代码>:

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

因此,根据定义,NULL 与零长度向量非常不同。零长度向量并不不存在。 NULL 实际上是对缺失或未设置的内容的包罗万象,但不是缺失,这是 NA 的工作。有一个例外,即零长度对列表,正如 @Owen 提到的。语言定义指出:

零长度配对列表是 NULL,正如 Lisp 中所期望的那样,但与零长度列表相反。

这突出了本例中的例外情况。

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

The R Language Definition has this on NULL:

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.

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:

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

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.

南街女流氓 2024-12-08 13:49:01

其他人有正确的答案,但我想补充一些好奇心。

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

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

列表(x=)$x

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

然后是“unbound”,你不能(据我所知)直接访问它,但可以使用 C:

SEXP getUnbound(void) {
    return R_UnboundValue;
}

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

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

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

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:

SEXP getUnbound(void) {
    return R_UnboundValue;
}

> x = .Call("getUnbound")
> x
Error: object 'x' not found
戒ㄋ 2024-12-08 13:49:01

这是部分答案,首先简单引用 R 语言定义指南:

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

我认为这意味着零长度向量可以具有属性,而 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

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

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.

I take that to mean that zero length vectors can have attributes, whereas NULL cannot:

> 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文