R时间段重叠

编程入门 行业动态 更新时间:2024-10-13 08:24:12
本文介绍了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时间段重叠

本文发布于:2023-06-11 03:53:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/626202.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间段

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!