POSIXct 对象的 as.vector() 有问题吗?

发布于 2025-01-14 03:15:56 字数 2060 浏览 3 评论 0原文

有趣的部分

让我们从设置开始(R 控制台):

> z <- Sys.time()
> z
[1] "2022-03-15 00:21:42 CET"
> class(z)
[1] "POSIXct" "POSIXt"

现在,情节(如笑话,而不是图形):

> as.numeric(z)
[1] 1647300102
> as.character(z)
[1] "2022-03-15 00:21:42"

现在,妙语(也如笑话,不是图形):

> as.vector(z, "numeric")
[1] 1647300102
> as.vector(z, "character")
[1] "1647300102.23863"

直观上,as.vector(z, "character") 应该返回与 as.character(z) 相同的结果,但事实并非如此。很有趣,不是吗?

严肃的部分

我想我明白为什么 as.vector()mode="character" 给出了自-以来的秒数-纪元而不是 as.character() 返回的人类可读的日期时间:POSIXct 类具有 as.character() 的实现代码>通用,但负责人并没有打扰为此类提供自定义 as.vector() 实现;因此,as.vector() 默认为 POSIXct 的基类(numeric),将日期时间打印为 >字符向量。

问题是:这应该被报告为一个错误吗?

我很抱歉在直接接触 R 核心开发团队之前在这里提出这个问题;原因如下:

问题部分

这应该是一个错误吗?在询问 R 核心人员之前,我宁愿先询问整个社区,因为 POSIXct 确实是 R 基本类型之一,但我不知道不知道这是否会困扰任何实际的 R 用户(除了我)。我的行为问题非常具体(即使用通用 as.vector(x, mode) 进行转换,而不是特定的 as.character(x)as.numeric(x)as.MaryPoppins(x) 等)

问题原因部分

我问问题的原因在这里:

  • CRAN R 中的错​​误报告本质上是说“如果你觉得意外,那么可能就在你的脑海中,除非有什么东西崩溃了”。这不是我所理解的“bug”。但也许我误解了。

  • 我想知道其他 R 用户是否会对上述行为感到惊讶。如果是,那么也许这可以被报告为“增强”而不是错误,但具有类似的结果(即将 as.vector 的行为修复为......理智?关于 POSIXct)。

  • 我还想知道当主要参数属于 POSIXct 类时,遗留代码在多大程度上依赖于 as.vector() 的现有行为。如果有很多用户依赖当前行为,那么也许我应该编写自己的 as.vector.POSIXct() 方法。

我感谢所有的评论,包括贬低的评论。

更新:元问题

以防万一您想到了通过 format()character 进行转换的解决方法...这不完全是重点。关键是:我们是否应该期望 base 命名空间中的类有一个一致的接口?好吧,现在我听起来像是一个被困在神奇宝贝球里的革命者……

The funny part

Let's start with the set-up (R console):

> z <- Sys.time()
> z
[1] "2022-03-15 00:21:42 CET"
> class(z)
[1] "POSIXct" "POSIXt"

Now, the plot (as in joke, not graphics):

> as.numeric(z)
[1] 1647300102
> as.character(z)
[1] "2022-03-15 00:21:42"

Now, the punch line (also as in joke, not graphics):

> as.vector(z, "numeric")
[1] 1647300102
> as.vector(z, "character")
[1] "1647300102.23863"

Intuitively, as.vector(z, "character") should return the same as as.character(z), but it doesn't. Funny, isn't it?

The serious part

I think I understand why as.vector() with mode="character" gives the number-of-seconds-since-epoch instead of the human-readable date-time returned by as.character(): the POSIXct class has an implementation for the as.character() generic, but the guys in charge didn't bother to provide a custom as.vector() implementation for this class; hence, as.vector() defaults to the base class of POSIXct (which is numeric) to print the date-time as a character vector.

The question is: should be this reported as a bug?

I apologize for asking this question here before going head-on to the R core development team; here's why:

The question part

Should this be a bug? I would rather ask the community at large first, before asking the R core guys, because POSIXct is -- indeed -- one of the base R types, but I don't know if this bothers any actual R users (excepting me). My problem with the behaviour is very specific (i.e. using a generic as.vector(x, mode) for conversion instead of specific as.character(x), as.numeric(x), as.MaryPoppins(x) etc.)

The reason-for-question part

The reasons for my asking the question are here:

  • The CRAN Bug reporting in R essentially says "if it seems unexpected to you, probably is in your head, unless something crashes". That is not my idea of "bug". But maybe I misunderstood.

  • I wonder if other R users would find the behaviour described above surprising. If yes, then maybe this can be reported as an "enhancement" instead as a bug, but with similar outcome (i.e. fixing the behaviour of as.vector to be... sane? about the POSIXct).

  • I wonder also how much legacy code relies on the existing behaviour of as.vector() when the main argument is of class POSIXct. If there are a lot of users counting on the current behaviour, then maybe I should write my own as.vector.POSIXct() methods.

I appreciate all the comments, including the disparaging ones.

Update: The meta-question

Just in case you've thought of a workaround using conversion to/from character via format()... is not exactly the point. The point is: should we expect a consistent interface from the classes in the base namespace or not? Ok, now I sound like a revolutionary confined in a Pokemon ball...

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文