这是我的代码:
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)
h
c
、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
>
但是,当我给出不同的日期/时间范围时。相交功能不起作用。例如:
如果我将变量 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)。有人可以帮我解决这个问题吗?
最佳答案
为了避免在评论中回答:
作为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 UTC
关于r - 如何在 R 中使用具有日期范围的 intersect() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40254577/