簡體   English   中英

如何獲取 R 中日期間隔的重疊值

[英]How to obtain overlapping values for intervals of dates in R

我有一個看起來像這樣的數據框:

w<-read.table(header=TRUE, text="
start.date   end.date   manager
2006-05-01   2007-04-30 a
2006-09-30   2007-12-31 b
1999-09-30   2007-12-31 c
2008-01-01   2012-04-30 d
2008-01-01   2020-02-28 e
2009-05-01   2016-04-08 f")

我想獲得一個 dataframe ,它返回該期間每個月哪些經理正在工作,例如

df<-read.table(header=TRUE, text="
month    manager1  manager2  manager3  manager4
01-2006  a         b         c         NA
02-2006  a         b         c         d
03-2006  b         c         d         NA
04-2006  b         d         NA        NA")

我首先定義了一個 function datseq ,它返回start.dateend.date之間的月份

datseq <- function(t1, t2) { 
  format(seq.Date(from = as.Date(t1,"%Y-%m-%d"), 
             to = as.Date(t2,"%Y-%m-%d"),by="month"), 
         "%m/%Y") 

但是我無法創建適當的循環來獲得所需的結果。 先謝謝大家的回復!

由於您只需要知道月級別的重疊而不是日級別的重疊,因此您可以認為經理從第 1 天開始並在月的最后一天離開。 這可以使用floor_date lubridate 中的lubridateceiling_date來實現。

library(lubridate)

w.extended <- w

w.extended$start.date <- floor_date(as.Date(w.extended$start.date), "month")
w.extended$end.date <- ceiling_date(as.Date(w.extended$end.date), "month") - 1

#List of months
timeperiod <- seq(min(w.extended$start.date),
                  by = "month", 
                  to = max(w.extended$end.date))

然后,您可以使用 package lubridatea %within% b ,它可以檢查日期是否在間隔列表內。 按照您提供的時間間隔,將此 function 應用於您的每個月。

df <- data.frame(t(sapply(timeperiod, 
                          function(x){
                            managersWorking <- x %within% interval(w.extended$start.date, 
                                                                   w.extended$end.date)
                            c(as.character(x), managersWorking)
                            })),
                 stringsAsFactors = F)

#Replace the 'character' format of columns to the appropriate one
df[-1] <- apply(df[-1], 2, as.logical)
df[,1]<- format(as.Date(df[,1]), "%Y/%m")

colnames(df) <- c("month", paste0("manager.", w$manager))

head(df)
#    month manager.a manager.b manager.c manager.d manager.e manager.f
#1 1999/09     FALSE     FALSE      TRUE     FALSE     FALSE     FALSE
#2 1999/10     FALSE     FALSE      TRUE     FALSE     FALSE     FALSE
#3 1999/11     FALSE     FALSE      TRUE     FALSE     FALSE     FALSE
#4 1999/12     FALSE     FALSE      TRUE     FALSE     FALSE     FALSE
#5 2000/01     FALSE     FALSE      TRUE     FALSE     FALSE     FALSE
#6 2000/02     FALSE     FALSE      TRUE     FALSE     FALSE     FALSE

原始數據:

w <- read.table(header=TRUE, text="
start.date   end.date   manager
2006-05-01   2007-04-30 a
2006-09-30   2007-12-31 b
1999-09-30   2007-12-31 c
2008-01-01   2012-04-30 d
2008-01-01   2020-02-28 e
2009-05-01   2016-04-08 f")
w
#  start.date   end.date manager
#1 2006-05-01 2007-04-30       a
#2 2006-09-30 2007-12-31       b
#3 1999-09-30 2007-12-31       c
#4 2008-01-01 2012-04-30       d
#5 2008-01-01 2020-02-28       e
#6 2009-05-01 2016-04-08       f

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM