可以有效地通過首先產生一個seq
uence的日期/時間,然後用cut
在其中找到每個值落在垃圾桶做到這一點:
set.seed(2)
dat <- Sys.time() + sort(runif(10, min=0, max=5*24*60*60))
dat
# [1] "2017-07-29 15:43:10 PDT" "2017-07-29 20:23:12 PDT" "2017-07-29 22:24:22 PDT" "2017-07-31 08:22:57 PDT"
# [5] "2017-07-31 18:13:06 PDT" "2017-07-31 21:01:10 PDT" "2017-08-01 12:30:19 PDT" "2017-08-02 04:14:03 PDT"
# [9] "2017-08-02 17:26:14 PDT" "2017-08-02 17:28:52 PDT"
sixs <- seq(as.POSIXct("2017-07-29 06:00:00", tz = "UTC"), as.POSIXct("2017-08-03 06:00:00", tz = "UTC"), by = "day")
sixs
# [1] "2017-07-29 06:00:00 UTC" "2017-07-30 06:00:00 UTC" "2017-07-31 06:00:00 UTC" "2017-08-01 06:00:00 UTC"
# [5] "2017-08-02 06:00:00 UTC" "2017-08-03 06:00:00 UTC"
cut(dat, sixs, label = FALSE)
# [1] 1 1 1 3 3 3 4 5 5 5
根據幫助頁面(?seq.POSIXt
),你可以選擇by="DSTday"
。
不確定你的意思是「我不想創建偏移量」。這是否適合您的需求? '庫(tidyverse); seq(as.POSIXct(Sys.time()),by =「5 min」,length.out = 50 * 288)%>%as_data_frame()%>% mutate(my_day = as.Date(value-as。 difftime(6,units =「hours」)))' – dmi3kno