简体   繁体   English

如何检查日期间隔是否在另一个间隔中

[英]How to check date interval is in another interval

I have created cols and rows with following code 我用以下代码创建了cols和行

    library(lubridate)    

date1<-ymd_hms("2000-01-01 05:30:00",tz = "US/Eastern")  
date2<- ymd_hms("2000-01-01 00:00:00",tz = "US/Eastern")

cols <- lapply(0:14, function(x){
  mapply(function(y,z){
    interval((date1+days(x)+minutes(y)), (date1+days(x)+minutes(y+z)))
  }, y = c(0,150,420,600,720,840,1080), z = c(600,570,600,600,600,600,600), SIMPLIFY = FALSE)
})

rows <- lapply(0:14, function(x){
  mapply(function(y,z){
    interval((date2+days(x)+minutes(y)), (date2+days(x)+minutes(y+z)))
  }, y = seq(0,1380,60), rep(c(60),24), SIMPLIFY = FALSE)
})  

The requirement is to create a matrix with cols and rows and with 1's and 0's. 要求是创建一个具有cols和row以及1和0的矩阵。 1's if row interval is in column interval else with 0's 如果行间隔在列间隔中则为1,否则为0

Intervals are composed of start : date of start, .Data : seconds of duration of the interval and tzone : time zone. 间隔由startstart日期, .Data :间隔持续时间的秒数和tzone :时区组成。

Asumming that you tzone is stable, you only need to reconstruct the start date of the two intervals you want to compare and check that one starts after the second and ends before the second. 假设您的tzone稳定,则只需重构要比较的两个间隔的开始日期,并检查一个间隔在第二个间隔之后开始并在第二个间隔之前结束。

Example: 例:

    isIncluded <- function(a,b){
      (a[[1]]@start>=b[[1]]@start & (a[[1]]@start+seconds(a[[1]]@.Data))<=(b[[1]]@start+seconds(b[[1]]@.Data)))
    }

 library(lubridate)    

date1<-ymd_hms("2000-01-01 05:30:00",tz = "US/Eastern")  
date2<- ymd_hms("2000-01-01 00:00:00",tz = "US/Eastern")

cols <- lapply(0:14, function(x){
  mapply(function(y,z){
    interval((date1+days(x)+minutes(y)), (date1+days(x)+minutes(y+z)))
  }, y = c(0,150,420,600,720,840,1080), z = c(600,570,600,600,600,600,600), SIMPLIFY = FALSE)
})

rows <- lapply(0:14, function(x){
  mapply(function(y,z){
    interval((date2+days(x)+minutes(y)), (date2+days(x)+minutes(y+z)))
  }, y = seq(0,1380,60), rep(c(60),24), SIMPLIFY = FALSE)
})  
    x <- cols[[1]][1]
    y <- cols[[1]][2]
    isIncluded(x,y) ## Is included x in y? False
    isIncluded(x,x) ## Is included x in x? True

The rest is just populating the matrix!
Best!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM