在R中只有日期和小时时使用ISOdatetime

时间:2015-02-01 01:04:09

标签: r date time

我的数据框的4列中有日期时间数据。我想用日期时间创建一个列。

一个小例子(对于我正在通过电话工作的格式不佳道歉)

2014 3 1 23 2.1
2014 3 2 0 4.7
2014 3 2 1 2.4

因此,上面给出的数据(最后一栏)分为三个时间段,每小时一小时,2014年3月1日2300(晚上11点),当晚午夜和2014年3月2日0100(凌晨1点)。

我想创建一个额外的列,其中包含“2009-03-01 23:00:00 GMT”。

我尝试使用

Mytimes <-with(my data,ISOdatetime(year,month,day,time,0,0)

我的列是年,月,日,时间,数据值,但得到的输出为“2009-03-01 EST”,没有小时数据。然后我可以将此列添加到我的数据框

我并不是特别担心时区,这就是示例的样子。

2 个答案:

答案 0 :(得分:0)

见MrFlick和Joshua的评论,午夜时间没有显示,但是在那里。

答案 1 :(得分:0)

这就是你想要的:

> require(lubridate)
> input <- read.table(text = "2014 3 1 23 2.1
+ 2014 3 2 0 4.7
+ 2014 3 2 1 2.4")
> # create date column
> input$date <- ymd_h(paste(input$V1, input$V2, input$V3, input$V4))
> 
> input
    V1 V2 V3 V4  V5                date
1 2014  3  1 23 2.1 2014-03-01 23:00:00
2 2014  3  2  0 4.7 2014-03-02 00:00:00
3 2014  3  2  1 2.4 2014-03-02 01:00:00