我用以下代码创建了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)
})
要求是创建一个具有cols和rows以及1和0的矩阵。如果行间隔在列间隔中则为1,否则为0
答案 0 :(得分:0)
间隔由start
:开始日期,.Data
:间隔持续时间的秒数和tzone
:时区组成。
假设tzone
是稳定的,则只需重构要比较的两个间隔的开始日期,并检查一个间隔在第二个间隔之后开始并在第二个间隔之前结束。
示例:
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!