统计指定日期范围内的人数

Count people present within specified date range

我有一个 df,其中包含个人的到达和离开日期以及他们的总停留时间 (los):

    arrive <- as.Date(c("2016/08/01","2016/08/03","2016/08/03","2016/08/04"))
    depart <- as.Date(c("2016/08/02","2016/08/07","2016/08/04", "2016/08/06"))
    people <- data.frame(arrive, depart)
    people$los <- people$depart - people$arrive
    View(people)

...和另一个包含开始和结束日期的 df。

    start <-seq(from=as.Date("2016/08/01"), to=as.Date("2016/08/08"), by="days")
    end <-seq(from=as.Date("2016/08/01"), to=as.Date("2016/08/08"), by="days") 
    range <- data.frame(start, end)
    View(range)

如何添加一个列 range$census 来计算每天有多少人在场?对于我的示例,我正在寻找的值如下:

range$census <- c(1,1,2,3,2,2,1,0)

我不确定的是如何对从一个 df 到另一个不同长度的 df 的值应用计算。到目前为止,这是我尝试过的方法:

    people$count <- 1 
    range$census <- sum(people$count[people$arrival <= range$start & people$depart >= range$end])

注意:在上面的示例中,start/end 日期是同一天,但我还需要查看更大的范围,其中 start/end 日期将相隔一个月或一年。

为什么需要范围内的 'end' 列?

这会起作用-

range$count <- rep(0, nrow(range))
sapply(seq(nrow(people)), function(x) 
       {
        range$count <<- range$count + range$start %in%
                        seq(people[x, "arrive"], people[x, "depart"], by = "day")
       })