POSIXct 对象的 as.vector() 有问题吗?
有趣的部分
让我们从设置开始(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 thePOSIXct
).I wonder also how much legacy code relies on the existing behaviour of
as.vector()
when the main argument is of classPOSIXct
. If there are a lot of users counting on the current behaviour, then maybe I should write my ownas.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论