跨时间窗口总计分钟的功能

时间:2016-10-31 09:16:30

标签: r

我正在创建一个函数,它总结了时间窗口(从8到9,从9到10)分布的time_1time_2之间的差异。

这是我的示例数据(请注意time_2总是大于time_1):

time_1 = c("08:20", "08:58", "09:30") 
time_2 = c("08:50", "09:20", "09:48") 
df = data.frame(time_1, time_2)

我写了以下函数(它还没有完成):

getTimePerIntervals <- function(df) {
  time_1_hour = as.numeric(substr(df$time_1,1,2))
  time_1_minutes = as.numeric(substr(df$time_1,4,5))
  time_2_hour = as.numeric(substr(df$time_2,1,2))
  time_2_minutes = as.numeric(substr(df$time_2,4,5))

  for (row in data_frame(time_1_hour,time_1_minutes,time_2_hour,time_2_minutes)){
    wt_8 = 0:
    wt_9 = 0
    if (row['time_1_hour']==8 & row['time_2_hour']==8)
    {
      wt_8 = row['time_2_minutes'] - row['time_1_minutes']
    }
    else if (row['time_1_hour']==9 & row['time_2_hour']==9)
    {
      wt_9 = row['time_2_minutes'] - row['time_1_minutes']
    }
    else if (row['time_1_hour']==8 & row['time_2_hour']==9)
    {
      wt_8 = (60 - row['time_1_hour'])
      wt_9 = row['time_1_minutes']
  }
# how to put wt_8 and wt_9 as columns of df?
df    
}

我的问题如下:

  1. 如何将wt_8wt_9转换为df的列?这里wt_8 表示从8到9的时间窗口,wt_9表示时间窗口 9到10.(请注意我想要这些变量,而不仅仅是 总体时间差异)
  2. 有没有更灵活的方法来做同样的事情?例如,假设时间窗口的数量大于2,那么应用“少手动”方法可能更好......

1 个答案:

答案 0 :(得分:0)

这是一种直接的方法。

time_1 = c("08:20", "08:58", "09:30") 
time_2 = c("08:50", "09:20", "09:48") 
df = data.frame(time_1, time_2)
time_1_hour = as.numeric(substr(df$time_1,1,2))
time_1_minutes = as.numeric(substr(df$time_1,4,5))
time_2_hour = as.numeric(substr(df$time_2,1,2))
time_2_minutes = as.numeric(substr(df$time_2,4,5))

window_names <- seq(min(time_1_hour), max(time_2_hour))
window_names <- paste(window_names,"-", window_names+1, sep="")
window_diffs <- matrix(rep(0, length(window_names)*nrow(df)), ncol=length(window_names))
colnames(window_diffs) <- window_names

for (i in seq.int(length(time_1_hour)))  {
    # first hour
    if(time_1_hour[i] < time_2_hour[i]) {
        wname <- paste(time_1_hour[i], "-", time_1_hour[i]+1, sep="")
        window_diffs[i, wname] <- 60 - time_1_minutes[i]
    }

    # full hours, not tested
    if(time_1_hour[i]+1 <= time_2_hour[i]-1) {
        wnames <- seq(time_1_hour[i]+1, time_2_hour[i]-1)
        wnames <- paste(wnames, "-", wnames+1, sep="")
        window_diffs[i, wnames] <- 60
    }

    # last hour
    if(time_1_hour[i] <= time_2_hour[i]) wname <- paste(time_2_hour[i], "-", time_2_hour[i]+1, sep="")
    if(time_1_hour[i] == time_2_hour[i] && time_1_minutes[i] < time_2_minutes[i]) 
        window_diffs[i, wname] <- time_2_minutes[i] - time_1_minutes[i]
    if(time_1_hour[i] < time_2_hour[i]) 
        window_diffs[i, wname] <- time_2_minutes[i]
}

df <- cbind(df, window_diffs)
return(df)

我相信这可以在速度方面得到改善。