使用R对不同时间段中的时间进行分类

时间:2018-09-17 10:18:51

标签: r time lubridate mutate

处理时差,并希望根据数据框中的不同时间创建时隙。例如,我的数据框中确实有一个单独的列,其中包含秒。我想做的是通过检查这些秒数是否落入任一类别即时隙中。

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")))

使用上述方法给我错误的分组。 谁能帮我解决这个问题!!!

2 个答案:

答案 0 :(得分:0)

如果您要提供一个最小的示例数据,将很有帮助。 也许可以帮到您吗?

# 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

答案 1 :(得分:0)

这是我用来获取输出的命令:

 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))