gpt4 book ai didi

R:按一天中的时间缩短日期时间

转载 作者:行者123 更新时间:2023-12-05 01:37:17 25 4
gpt4 key购买 nike

我有一个带有 POSIXct 日期时间的 data_frame。我现在想创建一个变量,将这些日期时间切割成时间段:1 -- [00:00:00, 08:00:00), 2 -- [08:00:00, 17:00:00) , 3 -- [17:00:00, 18:30:00), 4 -- [18:30:00, 00:00:00).

这是一些示例数据:

df_times = data_frame(
datetime = seq.POSIXt(
from = as.POSIXct(strftime("2016-01-01 00:00:00", format = "%Y-%m-%d :%H:%M:%S")),
by = "min",
length.out = 100000
),
value = rnorm(100000)
)

这是预期的输出:

> df_times
# A tibble: 100,000 × 3
datetime value band
<dttm> <dbl> <dbl>
1 2016-01-01 00:00:00 0.5855288 1
2 2016-01-01 00:01:00 0.7094660 1
3 2016-01-01 00:02:00 -0.1093033 1
4 2016-01-01 00:03:00 -0.4534972 1
5 2016-01-01 00:04:00 0.6058875 1
6 2016-01-01 00:05:00 -1.8179560 1
7 2016-01-01 00:06:00 0.6300986 1
8 2016-01-01 00:07:00 -0.2761841 1
9 2016-01-01 00:08:00 -0.2841597 1
10 2016-01-01 00:09:00 -0.9193220 1
# ... with 99,990 more rows

我已经尝试过 cut.POSIXt 但坚持要跟踪日期。理想的解决方案将使用 dplyr::recodeforcats::

最佳答案

这是我认为直接将问题的意图转化为代码的解决方案:

set.seed(12345)

# create a dataset
df_times = data_frame(
datetime = seq.POSIXt(
from = as.POSIXct("2016-01-01 00:00:00", format = "%Y-%m-%d %H:%M:%S"),
by = "min",
length.out = 100000
),
value = rnorm(100000)
) %>%
mutate(
time = times(format(datetime, "%H:%M:%S")),
cut(
time,
breaks = times(c(
"00:00:00",
"08:00:00",
"17:00:00",
"18:30:00",
"23:59:59"
)),
labels = c(
"1",
"2",
"3",
"4"
),
include.lowest = TRUE,
right = FALSE
)
)

关于R:按一天中的时间缩短日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39825411/

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