【发布时间】:2018-09-17 10:18:51
【问题描述】:
处理时差,并希望根据我的数据帧中的不同时间创建时隙。例如,我的数据框中确实有单独的列,其中包含秒数。我想要做的是通过检查这些秒数是否属于任何一个类别,即时隙。
timediff(in Sec) Waiting_slots
14589 >= 4 hours
11580 2 - 4 hours
11940 2 - 4 hours
date
2018-01-19 15:17:48 UTC--2018-01-19 19:20:57 UTC
2016-06-26 22:55:00 UTC--2016-06-27 02:08:00 UTC
2016-05-02 07:47:00 UTC--2016-05-02 11:06:00 UTC
等 所以,等待时段就像 4 小时 我必须像这样创建等待 _slots 但未能实现这一点,因为我不知道如何在 2 到 4 小时的时间间隔内做到这一点。 这个方法我试过了,
# timed <- c(2.1,2.2,2.3,2.4,2.5,2.6,2.7,2.8,2.9,3.0,3.1,3.2,3.3,3.4,3.5,3.6,3.7,3.8,3.9)
# AE_subset <- mutate(AE_subset, waiting_slots = ifelse(timediff < 2.0,"Less than 2 hours",
# ifelse(timediff %in% timed,"Between 2 - 4 hours",
# ifelse(timediff > 4.0,"More than 4 hours","check"))))
# AE_subset <- AE_subset %>% mutate(waiting_slots = replace(waiting_hours,waiting_hours== "check","Between 2 - 4 hours"))
我使用 Lubridate 的持续时间将秒转换为小时格式。
> duration(timediff = 14589)
[1] "14589s (~4.05 hours)"
ae <- ae %>% mutate(wait_slots = cut(ae$time_interval, breaks = c(7199,14400,121918,Inf),labels = c("Less than 2 hours","Between 2 to 4 hours","More than 4 hours")))
使用上述方法会给我错误的分组。 谁能帮我解决这个问题!!!
【问题讨论】:
-
你在找
cut吗? -
car::recode(timediff, "lo:7199='Less than 2 hours'; 7200:14400='2 to 4 hours';14401:hi='More than 4 hours') -
用于分组是的,但我的专栏是以秒为单位的。
-
@LAP 抱歉 切还是车?因为这辆车对我来说很新。
-
@chinsoon12 如果列包含日期和时间而不是秒怎么办?我会更新我的问题。