两个POSIXct时间之间的中点

时间:2016-01-08 12:27:48

标签: r posix

我想在stoptimestarttime之间取一个子午线或中点,然后添加一个带中点的新列。它可以四舍五入到最接近的秒。如何在R中做到这一点? stoptimestarttime都是POSIXct形式。有没有比将difftime分成两半然后添加到stoptime更简单的方法?

> head(data)
   bikeid end.station.id start.station.id diff.time            stoptime           starttime
24  23966            359              318      1505 2015-10-13 07:45:00 2015-10-13 08:10:05
28  23966            502              311      2072 2015-10-20 14:41:11 2015-10-20 15:15:43
58  17110            337              340      3338 2015-10-15 16:00:39 2015-10-15 16:56:17
74  23822            501              527      3478 2015-10-05 15:55:13 2015-10-05 16:53:11
83  16462            426              146      3368 2015-10-01 07:52:06 2015-10-01 08:48:14
89  23121            435              223      1499 2015-10-08 11:58:08 2015-10-08 12:23:07

> dput(head(data))
structure(list(bikeid = c("23966", "23966", "17110", "23822", 
"16462", "23121"), end.station.id = c("359", "502", "337", "501", 
"426", "435"), start.station.id = c("318", "311", "340", "527", 
"146", "223"), diff.time = c(1505, 2072, 3338, 3478, 3368, 1499
), stoptime = structure(c(1444740300, 1445370071, 1444942839, 
1444078513, 1443703926, 1444323488), class = c("POSIXct", "POSIXt"
), tzone = "EST"), starttime = structure(c(1444741805, 1445372143, 
1444946177, 1444081991, 1443707294, 1444324987), class = c("POSIXct", 
"POSIXt"), tzone = "EST")), .Names = c("bikeid", "end.station.id", 
"start.station.id", "diff.time", "stoptime", "starttime"), row.names =      c(24L, 28L, 58L, 74L, 83L, 89L), class = "data.frame")

1 个答案:

答案 0 :(得分:2)

您可以执行以下操作:

data$midtime <- as.POSIXct((as.numeric(stoptime) + as.numeric(starttime)) / 2, origin = '1970-01-01')