R时间段重叠 [英] R Time periods overlapping

查看:111
本文介绍了R时间段重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用R中的"lubridate"包,我可以确定两个时间段是否重叠.但是有没有一种有效的方法来计算它们重叠的天数. (例如,妇女在怀孕期间吸烟了多少天.怀孕期和吸烟期可能会完全重叠,部分重叠或根本不重叠)

With "lubridate" package in R, I can find out if two time periods overlapped. but Is there an efficient way to compute for how many days they overlapped. (for instance how many days a women smoked while pregnant. the pregnancy period and smoking period may overlap totally, partially or not at all)

这是一个有三个女人的例子:

Here is an example with three women:

preg_start<-as.Date(c("2011-01-01","2012-01-01","2013-01-01"))
preg_end<-preg_start+270 # end after 9 months
smoke_start<-as.Date(c("2011-02-01","2012-08-01","2014-01-01"))
smoke_end<-smoke_start+100 # all three smoked 100 days

data<-data.frame(cbind(preg_start,preg_end,smoke_start,smoke_end))

我想添加一个变量,说第一位妇女在怀孕期间吸烟了100天,第二位妇女在怀孕期间吸烟了30天,而第三位在怀孕期间没有吸烟.

I want to add a variable saying that the first woman smoked 100 days during pregnancy, the second smoked 30 days and the third did not smoke while pregnant.

推荐答案

使用interval创建怀孕和吸烟的时间间隔.然后计算这些间隔的intersect.据此,您可以计算天数period.

Use interval to create time intervals for pregnancy and smoking. Then calculate the intersect of these intervals. From that you can calculate the period in days.

library("lubridate")
preg_start<-as.Date(c("2011-01-01","2012-01-01","2013-01-01"))
preg_end<-preg_start+270 # end after 9 months
smoke_start<-as.Date(c("2011-02-01","2012-08-01","2014-01-01"))
smoke_end<-smoke_start+100 # all three smoked 100 days

smoke <- new_interval(smoke_start, smoke_end, tzone="UTC")
preg <- new_interval(preg_start, preg_end, tzone="UTC")
day(as.period(intersect(smoke, preg), "days"))

我在怀孕期间吸烟100、57和0天.

I get 100, 57 and 0 days of smoking during pregnancy.

这篇关于R时间段重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆