【发布时间】:2018-09-11 16:52:14
【问题描述】:
我希望将值格式数据的非标准更改(仅在 Value 更改时读取)格式化为标准的 30 秒间隔格式。
我有什么:df:
Timestamp Value
6/26/2018 0:00:06 10
6/26/2018 0:01:06 15
6/26/2018 0:02:15 20
和dput:
structure(list(Timestamp = c("6/26/2018 0:00:06", "6/26/2018 0:01:06",
"6/26/2018 0:02:15"), Value = c(10L, 15L, 20L)), .Names = c("Timestamp",
"Value"), class = "data.frame", row.names = c(NA, -3L))
我想要什么 formatted_df:
Timestamp Value
6/26/2018 0:00:30 10
6/26/2018 0:01:00 10
6/26/2018 0:01:30 15
6/26/2018 0:02:00 15
6/26/2018 0:02:30 20
我的尝试:
使用 lubridate 和 dplyr 中的函数,我的间隔是 30 秒的倍数,但它没有标准化到 30 秒:
formatted <- df %>% mutate(Timestamp_Date = as.POSIXct(Timestamp, tz = "US/Eastern", usetz = TRUE, format="%m/%d/%Y %H:%M:%S"),
rounded_timestamp = ceiling_date(Timestamp_Date, unit = "30 seconds"))
formatted:
Timestamp Value Timestamp_Date rounded_timestamp
6/26/2018 0:00:06 10 6/26/2018 0:00:06 6/26/2018 0:00:30
6/26/2018 0:01:06 15 6/26/2018 0:01:06 6/26/2018 0:01:30
6/26/2018 0:02:15 20 6/26/2018 0:02:15 6/26/2018 0:02:30
我认为lubridate 和dplyr 在这里会很有用,但我敢打赌data.table 可以做到。
【问题讨论】:
-
可以使用data.table的滚动连接。
标签: r dplyr data.table lubridate