gpt4 book ai didi

r - 两个 POSIXct 时间之间的中点

转载 作者:行者123 更新时间:2023-12-03 23:23:29 24 4
gpt4 key购买 nike

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

> head(data)   bikeid end.station.id start.station.id diff.time            stoptime           starttime24  23966            359              318      1505 2015-10-13 07:45:00 2015-10-13 08:10:0528  23966            502              311      2072 2015-10-20 14:41:11 2015-10-20 15:15:4358  17110            337              340      3338 2015-10-15 16:00:39 2015-10-15 16:56:1774  23822            501              527      3478 2015-10-05 15:55:13 2015-10-05 16:53:1183  16462            426              146      3368 2015-10-01 07:52:06 2015-10-01 08:48:1489  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")

最佳答案

您可以执行以下操作:

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

关于r - 两个 POSIXct 时间之间的中点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34677151/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com