R 05:00:00-28:59:59时间格式
我有一个数据集,其中R 05:00:00-28:59:59时间格式,r,date,posixct,R,Date,Posixct,我有一个数据集,其中time.start从5:00:00到28:59:59不等(即2013年1月1日28:00:00实际上是2013年1月2日04:00:00)。日期采用%d.%m.%Y格式 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:
time.start
从5:00:00到28:59:59不等(即2013年1月1日28:00:00实际上是2013年1月2日04:00:00)。日期采用%d.%m.%Y
格式
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
我想把它转换成普通的日期格式
dates$date <- paste(dates$Date,dates$Time.start, sep = " ")
dates$date <- as.POSIXct(strptime(dates$date, "%m.%d.%Y %H:%M:%S"))
dates$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"
df只是lukeAs溶液的一种改进:
with(df, as.POSIXct(Date, format="%d.%m.%Y")+
colSums(t(read.table(text=Time.start, sep=":",header=F))*c(3600,60,1)))
[1] "2013-01-01 22:13:07 EST" "2013-01-01 22:52:23 EST"
[3] "2013-01-01 23:34:06 EST" "2013-01-01 23:44:25 EST"
[5] "2013-01-02 03:18:48 EST" "2013-01-02 04:41:04 EST"
使用润滑油润滑
:
with(dates, mdy(Date) + hms(Time.start))
生成:
[1] "2013-01-01 22:13:07 UTC" "2013-01-01 22:52:23 UTC"
[3] "2013-01-01 23:34:06 UTC" "2013-01-01 23:44:25 UTC"
[5] "2013-01-02 03:18:48 UTC" "2013-01-02 04:41:04 UTC"
使用scan
直接从日期读取整数怎么样。没有分裂,没有强迫。我真的很喜欢你的想法。你的解决方案比@akrun快。我有一个180 000行的数据帧,创建向量需要2.5秒,而使用和
命令需要31秒。现在我对这个脚本的函数有问题=(让我想起了日本电视节目时间表的时间范围,当他们使用非24小时的时间(例如,“深夜3:00”表示第二天凌晨3点)时,这会变得很愚蠢。事实上,你会看到酒吧广告说他们开放到“25:00”凌晨1点等。通过在数据库中抵消-5小时,并在实际使用数据时加上+5来正常化。:[使用-5,您将处于00:00:00-23:59:59范围内。]
他们到底为什么要用05:00:00-28:59:59?@Panzercrisis:如果一家公司每天从凌晨1:00-7:00关门,每周的工作时间从周一00:00:00.01一直到周日23:59:59.99,那么周日下午5:00开始,周一凌晨1:00结束的工作班次将有七个小时的时间可归因于一份工资和一项属性将轮班记录为周日从17:00运行到25:00可以避免这个问题。