计算指定日期范围内的人员 [英] Count people present within specified date range
问题描述
我有一个df,其中包含个人的到来&出发日期及其总停留时间(丢失):
I have one df containing individuals' arrival & departure dates and their total length of stay (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)
...和另一个包含start&的df结束日期.
...and another df containing start & end dates.
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)
如何添加列范围普查来计算每天有多少人?对于我的示例,我要查找的值如下:
How can I add a column range$census to count how many people were present each day? For my example, the values I'm looking for would be as follows:
range$census <- c(1,1,2,3,2,2,1,0)
我不确定是如何对从一个df到另一个长度不同的df的值应用计算.到目前为止,这是我尝试过的:
What I am not sure of is how to apply a calculation on values from one df to another df of a different length. Here's what I've tried so far:
people$count <- 1
range$census <- sum(people$count[people$arrival <= range$start & people$depart >= range$end])
注意:在上面的示例中,开始/结束日期是同一天,但是我还需要查看更大的范围,其中开始/结束日期相隔一个月或一年.
Note: in example above the start/end dates are the same day, but I will also need to look at larger ranges, where the start/end dates will be a month or a year apart.
推荐答案
为什么需要范围内的"end"列?
Why do you need the 'end' column in range?
这将起作用-
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")
})
这篇关于计算指定日期范围内的人员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!