sapply 无法正确处理日期 [英] sapply cannot handle date correctly
问题描述
可能的重复:
为什么 mapply 不返回日期对象?
我遇到了以下奇怪的问题,当我转换一个字符串时没有问题:
i got the following bizzar problem, I have no problem when I conver one single string to date:
as.Date(alldays[1])
as.Date(alldays[1])
[1] "3-04-20"
但是,当我使用 sapply 或 lapply 时,我得到了一个很大的负数,有人知道为什么会这样吗?谢谢!
however, when I use sapply or lapply, I got a big negative number, does anyone know why it is like this? Thanks!
> (sapply(alldays[1:4], as.Date))
03-04-2012 02-04-2012 30-03-2012 29-03-2012
-718323 -718688 -708492 -708857
<小时>
大家好,我发现问题了,非常感谢您的帮助!
Hi guys, i found the problem, thanks a lot for your help!
推荐答案
sapply
应用 simplify2array
(请参阅文档).如果您查看 simplify2array
的代码,您会看到 r <- as.vector(unlist(x, recursive = FALSE))
.as.vector
删除所有属性(再次参见文档),包括Date"类.
sapply
applies simplify2array
(see the documentation). If you look at the code of simplify2array
you see r <- as.vector(unlist(x, recursive = FALSE))
. as.vector
removes all attributes (again see the documentation) including class "Date".
使用as.Date(alldays[1:4],'%d-%m-%Y)
.
这篇关于sapply 无法正确处理日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!