使用日期时间POSIXlt创建R数据表时出错 [英] Error creating R data.table with date-time POSIXlt

查看:127
本文介绍了使用日期时间POSIXlt创建R数据表时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建带有日期时间列的data.table时遇到的问题:

 > mdt < -  data.table(id = 1:3,d = strptime(c(06:02:36,06:02:48,07:03:12),%H:% M:%S))
> class(mdt)
[1]data.tabledata.frame
> print(mdt)
`rownames`中的错误<-`(`* tmp *`,value = paste(format(rn,right = TRUE),:
'dimnames'[1]到数组范围

输入一个帧号,或0退出

1:print(list(id = 1:3,d = list(sec = c 48,12),min = c(2,2,3),hour = c(6,6,7),mday = c(31,
2:print.data.table :3,d = list(sec = c(36,48,12),min = c(2,2,3),hour = c(6,6,7),m
3:`rownames& -`(`* tmp *`,value = paste(format(rn,right = TRUE),:,sep =))


创建为data.frame并转换为data.table工作原理。

  mdf > print(mdf)
id d
1 1 2014-01-31 06:02:36
2 2 2014-01-31 06:02:48
3 3 2014-01-31 07:03:12
> mdt< - as.data.table(mdf)
> print(mdt)
id d
1:1 2014-01-31 06:02:36
2:2 2014-01-31 06:02:48
3:3 2014-01- 31 07:03:12
> class(mdt)
[1]data.tabledata.frame

我缺少什么或是错误?如果一个错误,我在哪里报告?



注意我使用R版本3.0.0,我看到一些警告。使用版本3.0.2构建的软件包。可能是问题吗?我应该升级R本身吗?

解决方案

格式化响应Blue Magister的评论(感谢这么多),data.table由于性能原因而不支持POSIXlt数据类型 - 请参阅尽可能建议的将字符串转换为IDateTime 复制



因此,要做的是将时间作为ITime(由data.table提供的类型)或日期时间(或仅日期)作为POSIXct,具体取决于日期信息是否重要:

 > mdt > print(mdt)
id d
1:1 06:02:36
2:2 06:02:48
3:3 07:03:12
> mdt < - data.table(id = 1:3,d = as.POSIXct(strptime(c(06:02:36,06:02:48,07:03:12), %H:%M:%S)))
> print(mdt)
id d
1:1 2014-01-31 06:02:36
2:2 2014-01-31 06:02:48
3: 3 2014-01-31 07:03:12

另外注意,如果有人可以受益它,我想创建日期&时间从我的输入数据与日期&时间在单独的字段。
我发现有用的是学习(见?ITime),可以添加时间ITime到日期时间POSIXct,并得到一个日期时间POSIXct如下:

 > mdt<  -  as.POSIXct(2014-01-31)+ as.ITime(06:02:36)
> print(mdt)
[1]2014-01-31 06:02:36 EST
> class(mdt)
[1]POSIXctPOSIXt


Problem creating data.table with date-time column:

> mdt <- data.table(id=1:3, d=strptime(c("06:02:36", "06:02:48", "07:03:12"), "%H:%M:%S"))
> class(mdt)
[1] "data.table" "data.frame"
> print(mdt)
Error in `rownames<-`(`*tmp*`, value = paste(format(rn, right = TRUE),  : 
  length of 'dimnames' [1] not equal to array extent

Enter a frame number, or 0 to exit   

1: print(list(id = 1:3, d = list(sec = c(36, 48, 12), min = c(2, 2, 3), hour = c(6, 6, 7), mday = c(31,
2: print.data.table(list(id = 1:3, d = list(sec = c(36, 48, 12), min = c(2, 2, 3), hour = c(6, 6, 7), m
3: `rownames<-`(`*tmp*`, value = paste(format(rn, right = TRUE), ":", sep = ""))

Create as data.frame and convert to data.table works!

> mdf <- data.frame(id=1:3, d=strptime(c("06:02:36", "06:02:48", "07:03:12"), "%H:%M:%S"))
> print(mdf)
  id                   d
1  1 2014-01-31 06:02:36
2  2 2014-01-31 06:02:48
3  3 2014-01-31 07:03:12
> mdt <- as.data.table(mdf)
> print(mdt)
   id                   d
1:  1 2014-01-31 06:02:36
2:  2 2014-01-31 06:02:48
3:  3 2014-01-31 07:03:12
> class(mdt)
[1] "data.table" "data.frame"

Am I missing anything or is it bug? If a bug, where do I report it?

Note I use R version 3.0.0 and I see some warnings re. packages built with version 3.0.2. Can it be the problem? Should I upgrade R itself? Everything else I do seems to be working though.

解决方案

Formatting response from Blue Magister's comment (thanks so much), data.table does not support POSIXlt data types for performance reason -- see cast string to IDateTime as suggested as possible duplicate.

So the way to go is to cast time as ITime (type provided by data.table) or date-time (or date only) as POSIXct, depending upon whether date info is important or not:

> mdt <- data.table(id=1:3, d=as.ITime(strptime(c("06:02:36", "06:02:48", "07:03:12"), "%H:%M:%S")))
> print(mdt)
   id        d
1:  1 06:02:36
2:  2 06:02:48
3:  3 07:03:12
> mdt <- data.table(id=1:3, d=as.POSIXct(strptime(c("06:02:36", "06:02:48", "07:03:12"), "%H:%M:%S")))
> print(mdt)
   id                   d
1:  1 2014-01-31 06:02:36
2:  2 2014-01-31 06:02:48
3:  3 2014-01-31 07:03:12

As an extra note in case someone can benefit from it, I wanted to create date & time from my input data with date & time in separate fields. I found it useful to learn (see ?ITime) that one can add time ITime to date-time POSIXct and get a date-time POSIXct as follows:

> mdt <- as.POSIXct("2014-01-31") + as.ITime("06:02:36")
> print(mdt)
[1] "2014-01-31 06:02:36 EST"
> class(mdt)
[1] "POSIXct" "POSIXt" 

这篇关于使用日期时间POSIXlt创建R数据表时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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