R:将 POSIXct 类分配给数据框
当我将 POSIXct 对象分配给数据帧时,它会将其转换为等效的整数,例如:
> x<-as.data.frame(matrix(nrow=1,ncol=2))
> x
V1 V2
1 NA NA
> x[1,1]<-as.POSIXct("2010-12-07 08:00:00")
> x
V1 V2
1 1291708800 NA
有没有办法阻止这种行为,或者在完成所有分配后轻松将整数转换回 POSIXct?
When I assign a POSIXct object into a data frame, it converts it into the integer equivalent, for example:
> x<-as.data.frame(matrix(nrow=1,ncol=2))
> x
V1 V2
1 NA NA
> x[1,1]<-as.POSIXct("2010-12-07 08:00:00")
> x
V1 V2
1 1291708800 NA
Is there a way to stop this behaviour, or to easily convert the integers back into POSIXct once I have done all the assignments?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建列后需要对其进行转换。
查看如何为它分配一个
类
。矩阵只能有一种数据类型。当然,我不清楚为什么你一开始就把它创建为一个空矩阵。您可以轻松完成:
You need to convert the column after you have already created it.
See how it was already assigned a
class
. Amatrix
can only have one data type.Of course, it's unclear to me why you created it as an empty matrix to begin with. You can have just as easily have done:
我有完全相同的问题。为了解决这个问题,我使用
as.POSIXct()
声明了数据框的列类。示例:
但
有趣的是,即使初始默认列类是“逻辑”:
您可以向其中写入字符:
但是,像 POSIXct 一样,您不能写入因子:
I had exactly the same problem. To fix this, I declared the column class of the data frame using
as.POSIXct()
.Example:
But
Interestingly, even though the initial default column class is
"logical"
:You can write characters to it:
However, like
POSIXct
, you cannot write factors: