按固定的时间间隔对POSIXct进行舍入以避免滞后[R]

gojuced7  于 2023-05-11  发布在  其他
关注(0)|答案(1)|浏览(171)

我有一个包含名为date的POSIX列的数据集。观察(行)以5分钟的间隔彼此分开。不幸的是,在一定时间之后,两个观测之间出现滞后,延迟了 Dataframe 的其余部分。在该示例中,2022-05-31 09:40:00-> 2022-05-31 09:48:27,而不是2022-05-31 09:45:00
我想纠正它有回“分钟”值的POSIX ct整理由5或0(秒是没有必要的,可以删除)。
最终目的是将数据集与另一个数据集合并。然后,我宁愿有一个由于滞后校正而丢失的值,而不是我的数据集的整个部分由于不可能合并值而绝望。
我的数据集的一个例子:

structure(list(date = structure(c(1653983700, 1653984000, 1653984300, 
1653984600, 1653984900, 1653985200, 1653985500, 1653985800, 1653986100, 
1653986400, 1653986700, 1653987000, 1653987300, 1653987600, 1653987900, 
1653988200, 1653988500, 1653988800, 1653989100, 1653989400, 1653989700, 
1653990000, 1653990507, 1653990807, 1653991107, 1653991407, 1653991707, 
1653992007, 1653992307, 1653992607, 1653992907), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), `Pressure[cmH2O]` = c("983.800", "983.917", "983.800", 
"984.383", "984.325", "984.033", "984.208", "984.325", "984.617", 
"984.208", "984.325", "984.208", "984.325", "984.092", "984.383", 
"984.208", "984.383", "984.500", "984.500", "984.500", "984.500", 
"971.083", "972.367", "984.967", "985.258", "984.792", "984.675", 
"984.792", "984.792", "984.967", "984.967")), row.names = c(NA, 
-31L), class = c("tbl_df", "tbl", "data.frame"))
m528fe3b

m528fe3b1#

使用lubridate::round_date。

lubridate::round_date(DF$date, "5 minutes")

结果

[1] "2022-05-31 07:55:00 UTC" "2022-05-31 08:00:00 UTC"
 [3] "2022-05-31 08:05:00 UTC" "2022-05-31 08:10:00 UTC"
 [5] "2022-05-31 08:15:00 UTC" "2022-05-31 08:20:00 UTC"
 [7] "2022-05-31 08:25:00 UTC" "2022-05-31 08:30:00 UTC"
 [9] "2022-05-31 08:35:00 UTC" "2022-05-31 08:40:00 UTC"
[11] "2022-05-31 08:45:00 UTC" "2022-05-31 08:50:00 UTC"
[13] "2022-05-31 08:55:00 UTC" "2022-05-31 09:00:00 UTC"
[15] "2022-05-31 09:05:00 UTC" "2022-05-31 09:10:00 UTC"
[17] "2022-05-31 09:15:00 UTC" "2022-05-31 09:20:00 UTC"
[19] "2022-05-31 09:25:00 UTC" "2022-05-31 09:30:00 UTC"
[21] "2022-05-31 09:35:00 UTC" "2022-05-31 09:40:00 UTC"
[23] "2022-05-31 09:50:00 UTC" "2022-05-31 09:55:00 UTC"
[25] "2022-05-31 10:00:00 UTC" "2022-05-31 10:05:00 UTC"
[27] "2022-05-31 10:10:00 UTC" "2022-05-31 10:15:00 UTC"
[29] "2022-05-31 10:20:00 UTC" "2022-05-31 10:25:00 UTC"
[31] "2022-05-31 10:30:00 UTC"

或者四舍五入到分钟。

round(DF$date, "mins")

结果

[1] "2022-05-31 07:55:00 UTC" "2022-05-31 08:00:00 UTC"
 [3] "2022-05-31 08:05:00 UTC" "2022-05-31 08:10:00 UTC"
 [5] "2022-05-31 08:15:00 UTC" "2022-05-31 08:20:00 UTC"
 [7] "2022-05-31 08:25:00 UTC" "2022-05-31 08:30:00 UTC"
 [9] "2022-05-31 08:35:00 UTC" "2022-05-31 08:40:00 UTC"
[11] "2022-05-31 08:45:00 UTC" "2022-05-31 08:50:00 UTC"
[13] "2022-05-31 08:55:00 UTC" "2022-05-31 09:00:00 UTC"
[15] "2022-05-31 09:05:00 UTC" "2022-05-31 09:10:00 UTC"
[17] "2022-05-31 09:15:00 UTC" "2022-05-31 09:20:00 UTC"
[19] "2022-05-31 09:25:00 UTC" "2022-05-31 09:30:00 UTC"
[21] "2022-05-31 09:35:00 UTC" "2022-05-31 09:40:00 UTC"
[23] "2022-05-31 09:48:00 UTC" "2022-05-31 09:53:00 UTC"
[25] "2022-05-31 09:58:00 UTC" "2022-05-31 10:03:00 UTC"
[27] "2022-05-31 10:08:00 UTC" "2022-05-31 10:13:00 UTC"
[29] "2022-05-31 10:18:00 UTC" "2022-05-31 10:23:00 UTC"
[31] "2022-05-31 10:28:00 UTC"

数据

DF <- structure(list(date = structure(c(1653983700, 1653984000, 1653984300, 
1653984600, 1653984900, 1653985200, 1653985500, 1653985800, 1653986100, 
1653986400, 1653986700, 1653987000, 1653987300, 1653987600, 1653987900, 
1653988200, 1653988500, 1653988800, 1653989100, 1653989400, 1653989700, 
1653990000, 1653990507, 1653990807, 1653991107, 1653991407, 1653991707, 
1653992007, 1653992307, 1653992607, 1653992907), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), `Pressure[cmH2O]` = c("983.800", "983.917", "983.800", 
"984.383", "984.325", "984.033", "984.208", "984.325", "984.617", 
"984.208", "984.325", "984.208", "984.325", "984.092", "984.383", 
"984.208", "984.383", "984.500", "984.500", "984.500", "984.500", 
"971.083", "972.367", "984.967", "985.258", "984.792", "984.675", 
"984.792", "984.792", "984.967", "984.967")), row.names = c(NA, 
-31L), class = c("tbl_df", "tbl", "data.frame"))

相关问题