使用 R 将时间分类为不同的时间槽
Categorising Time in different slots using R
处理时差问题,想根据我的数据帧中的不同时间创建时隙。例如,我的数据框中确实有单独的列,其中包含秒数。我想要做的是检查这些秒数是否属于任何一个类别,即时隙。
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
等
所以,等待时段就像 <=2 小时、2 - 4 小时、>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")))
使用上述方法给我错误的分组。
谁能帮我解决这个问题!!!
如果您提供手头数据的最小示例,将会很有帮助。
也许这可以帮到你?
# generate data with random timestamps
timeStart <- sort(as.POSIXct(sample(1000:10000,20),origin="1970-01-01"))
timeEnd <- timeStart + as.difftime(seq(0,10,length.out = 20),units="hours")
data <- data.frame(start = timeStart, end = timeEnd)
# function for time categorisation
timeCategory <- function(t0,t1){
diffTime <- difftime(t1,t0,units = "hours")
if(diffTime < 2){
return(1)
}else if(2<= diffTime && diffTime < 4){
return(2)
}else{
return(3)
}
}
#apply function to data
timeCat <- apply(data,1,function(t)timeCategory(t[1],t[2]))
timeCat
这是我用来获取输出的命令:
DF<- DF %>% mutate(waiting_hours = cut(DF$ELAPSED_MINS_ARRIVAL_TO_DEPARTURE, breaks = c(0,119,239,2031),labels = c("Less than 2 hours","2 to 4 hours","More than 4 hours"),include.lowest = TRUE))
处理时差问题,想根据我的数据帧中的不同时间创建时隙。例如,我的数据框中确实有单独的列,其中包含秒数。我想要做的是检查这些秒数是否属于任何一个类别,即时隙。
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
等 所以,等待时段就像 <=2 小时、2 - 4 小时、>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")))
使用上述方法给我错误的分组。 谁能帮我解决这个问题!!!
如果您提供手头数据的最小示例,将会很有帮助。 也许这可以帮到你?
# generate data with random timestamps
timeStart <- sort(as.POSIXct(sample(1000:10000,20),origin="1970-01-01"))
timeEnd <- timeStart + as.difftime(seq(0,10,length.out = 20),units="hours")
data <- data.frame(start = timeStart, end = timeEnd)
# function for time categorisation
timeCategory <- function(t0,t1){
diffTime <- difftime(t1,t0,units = "hours")
if(diffTime < 2){
return(1)
}else if(2<= diffTime && diffTime < 4){
return(2)
}else{
return(3)
}
}
#apply function to data
timeCat <- apply(data,1,function(t)timeCategory(t[1],t[2]))
timeCat
这是我用来获取输出的命令:
DF<- DF %>% mutate(waiting_hours = cut(DF$ELAPSED_MINS_ARRIVAL_TO_DEPARTURE, breaks = c(0,119,239,2031),labels = c("Less than 2 hours","2 to 4 hours","More than 4 hours"),include.lowest = TRUE))