为什么将Date返回为“ double”类型? [英] Why is Date is being returned as type 'double'?

查看:129
本文介绍了为什么将Date返回为“ double”类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用R中的 as.Date 函数时遇到了一些麻烦。我有一个从.csv文件中读取的日期向量以整数或字符的形式出现(取决于我在文件中的读取方式,但这似乎与问题无关),格式为%m /%d /%Y

I'm having some trouble working with the as.Date function in R. I have a vector of dates that I'm reading in from a .csv file that are coming in as a factor of integers or as character (depending on how I read in the file, but this doesn't seem to have anything to do with the issue), formatted as %m/%d/%Y.

我正在逐行浏览文件,提取日期字段,并尝试使用以下代码将其转换为在其他地方使用:

I'm going through the file row by row, pulling out the date field and trying to convert it for use elsewhere using the following code:

tmpDtm <- as.Date(as.character(tempDF$myDate), "%m/%d/%Y")

这似乎给了我我想要的东西,例如,如果我这样做的起始值为12/30 / 2014年,我得到了返回值 2014-12-30 。但是,如果我使用 typeof()检查此值,R会告诉我它的数据类型为双精度。此外,如果我尝试将其绑定到其他值,并使用 c() cbind(),在数据帧中,它最终被存储为16434,在我看来,它类似于某种日期的内部存储值。我很确定这也是事实,因为如果我尝试使用 as.Date()再次转换该值,则会引发要求输入原点的错误。

This seems to give me what I want, for example, if I do this to a starting value of 12/30/2014, I get the value "2014-12-30" returned. However, if I examine this value using typeof(), R tells me that it its data type is 'double'. Additionally, if I try to bind this to other values and store it in a data frame using c() or cbind(), in the data frame, it winds up being stored as 16434, which looks to me like some sort of different internal storage value of a date. I'm pretty sure that's what it is too because if I try to convert that value again using as.Date(), it throws an error asking for an origin.

因此,有两个问题:这是否符合预期?如果是这样,是否有更合适的方法来转换日期,以使我最终得到日期类型的对象?

So, two questions: Is this as expected? If so, is there a more appropriate way to convert a date so that I actually end up with a date-typed object?

谢谢

推荐答案

日期在内部以double表示,如下面的示例所示:

Dates are internally represented as double, as you can see in the following example:

> typeof(as.Date("09/12/16", "%m/%d/%y"))
[1] "double"

它仍标记为班级日期,例如

it is still marked a class Date, as in

> class(as.Date("09/12/16", "%m/%d/%y"))
[1] "Date"

并且由于它是双精度型,因此可以使用它进行计算。但是因为它是Date类的,所以这些计算会导致Dates:

and because it is a double, you can do computations with it. But because it is of class Date, these computations lead to Dates:

> as.Date("09/12/16", "%m/%d/%y") + 1
[1] "2016-09-13"
> as.Date("09/12/16", "%m/%d/%y") + 31
[1] "2016-10-13"






编辑
我要了 c() cbind(),因为它们可能会伴随一些奇怪的行为。请参见下面的示例,其中在 c 中切换顺序不会更改结果的类型,而是更改结果的类:


EDIT I have asked for c() and cbind(), because they can be assciated with some strange behaviour. See the following example, where switching the order within c changes not the type but the class of the result:

> c(as.Date("09/12/16", "%m/%d/%y"), 1)
[1] "2016-09-12" "1970-01-02"
> c(1, as.Date("09/12/16", "%m/%d/%y"))
[1]     1 17056

> class(c(as.Date("09/12/16", "%m/%d/%y"), 1))
[1] "Date"
> class(c(1, as.Date("09/12/16", "%m/%d/%y")))
[1] "numeric"






编辑2- c() cbind 强制对象属于一种类型。第一次编辑显示了强制异常,但通常,向量必须是一种共享类型。 cbind 具有此行为,因为它强制转换为矩阵,进而强制转换为单一类型。


EDIT 2 - c() and cbind force objects to be of one type. The first edit shows an anomaly of coercion, but generally, the vector must be of one shared type. cbind shares this behavior because it coerces to matrix, which in turn coerces to a single type.

更多帮助在 typeof class 上请参阅

For more help on typeof and class see this link

这篇关于为什么将Date返回为“ double”类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆