05:00:00-28:59:59时间格式 [英] 05:00:00 - 28:59:59 time format

查看:106
本文介绍了05:00:00-28:59:59时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,其中 time.start 从5:00:00到28:59:59(即01.01.2013 28:00:00实际上是02.01。 2013 04:00:00)。日期采用%d。%m。%Y 格式。

I have dataset where time.start vary from 5:00:00 to 28:59:59 (i.e. 01.01.2013 28:00:00 is actually 02.01.2013 04:00:00). Dates are in %d.%m.%Y format.

      Date Time.start   
01.01.2013   22:13:07
01.01.2013   22:52:23
01.01.2013   23:34:06
01.01.2013   23:44:25
01.01.2013   27:18:48
01.01.2013   28:41:04

我想将其转换为普通日期格式。

I want to convert it to normal date format.

dates$date <- paste(dates$Date,dates$Time.start, sep = " ")
dates$date <- as.POSIXct(strptime(dates$date, "%m.%d.%Y %H:%M:%S"))

但是显然我有 NA 时间> 23:59:59

But obviously I have NA for time > 23:59:59

我应如何修改我的代码?

How should I modify my code?

推荐答案

例如将时间添加为日期的秒数:

E.g. add the time as seconds to the date:

df <- read.table(header=T, text="      Date Time.start   
01.01.2013   22:13:07
01.01.2013   22:52:23
01.01.2013   23:34:06
01.01.2013   23:44:25
01.01.2013   27:18:48
01.01.2013   28:41:04", stringsAsFactors=FALSE)

as.POSIXct(df$Date, format="%d.%m.%Y") +
  sapply(strsplit(df$Time.start, ":"), function(t) {
    t <- as.integer(t)
    t[3] + t[2] * 60 + t[1] * 60 * 60
  })

# [1] "2013-01-01 22:13:07 CET" "2013-01-01 22:52:23 CET" "2013-01-01 23:34:06 CET"
# [4] "2013-01-01 23:44:25 CET" "2013-01-02 03:18:48 CET" "2013-01-02 04:41:04 CET"

这篇关于05:00:00-28:59:59时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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