下面是我的代码:
a<-"2015-12-13 09:00:00"
b<-"2015-12-13 12:00:00"
c<-interval(a,b)
d<-"2015-12-13 09:00:00"
e<-"2015-12-13 12:00:00"
f<-interval(d,e)
h<-intersect(c,f)
hc、f和h的值为:
> h
[1] 10800
> c
[1] 2015-12-13 09:00:00 UTC--2015-12-13 12:00:00 UTC
> f
[1] 2015-12-13 09:00:00 UTC--2015-12-13 12:00:00 UTC
> 但是,当我给出不同的日期/时间范围时。intersect函数不起作用。例如:如果我将变量d和e的时间从09:00:00-12:00:00更改为09:00:00 - 10:00:00,我希望intersect函数能为我提供常见的1小时的分钟数。
a<-"2015-12-13 09:00:00"
b<-"2015-12-13 12:00:00"
c<-interval(a,b)
d<-"2015-12-13 09:00:00"
e<-"2015-12-13 10:00:00"
f<-interval(d,e)
h<-intersect(c,f)
h输出:
numeric(0)
> c
[1] 2015-12-13 09:00:00 UTC--2015-12-13 12:00:00 UTC
> f
[1] 2015-12-13 09:00:00 UTC--2015-12-13 10:00:00 UTC我得到了数字(0)。谁能帮帮我。
发布于 2020-03-17 15:59:58
为了避免在评论中回答:
正如J_F上面提到的,您需要使用lubridate中的intersect函数。如果因为需要来自lubridate和dplyr的intersect而遇到问题,可以使用双冒号::。
双冒号::用于指定从哪个包调用函数并克服屏蔽。所以在你的代码中:
a<-"2015-12-13 09:00:00"
b<-"2015-12-13 12:00:00"
c<-interval(a,b)
d<-"2015-12-13 09:00:00"
e<-"2015-12-13 10:00:00"
f<-interval(d,e)
h<-lubridate::intersect(c,f)
h
[1] 2015-12-13 09:00:00 UTC--2015-12-13 10:00:00 UTChttps://stackoverflow.com/questions/40254577
复制相似问题