如何计算不在列表中的日期 [英] How to count the dates that are not in a list
问题描述
我在与客户的两个日期(date1 date2)和到达日期之间有一个数据框。
I have a dataframe between two dates (date1 date2) with clients and the date of arrive.
date1<- "2019-07-29"
date2<- "2019-08-08"
clients<-data.frame(id= c(1:10),
arrive=c("2019-07-31", "2019-07-29", "2019-08-01",
"2019-08-03", "2019-08-05", "2019-08-08",
"2019-08-02", "2019-08-06", "2019-07-29",
"2019-08-02"),
hotel= c(rep(900067, 5), rep(9001649,5)))
我想计算两次约会之间的间隔时间,即每家酒店没有新客户的天数。
I want to count between does dates, how many days each hotel did not have a new clients.
酒店900067在以下几天没有新客户:2019-07-30、2019-08-02、2019-08-04、2019-08-06, 2019-08-07,2019-08-08。这是介于date1和date2之间的6天,没有任何新客户。
Hotel 900067 did not have new clients the following days: 2019-07-30, 2019-08-02, 2019-08-04, 2019-08-06, 2019-08-07, 2019-08-08. This are 6 days between date1 and date2 without any new client.
数据框结果应类似于:
Result<- data.frame(hotel= c(900067, 9001649),
days_without_new_clients= c(6, 7))
谢谢。
推荐答案
也许您可以创建一个包含所有酒店日期的数据框,然后查看<$中不存在的日期c $ c> clients data。
Perhaps you could create a data frame containing all hotel-dates and then see how many that do not exist in the clients
data.
library(dplyr)
all_hotel_dates <- expand.grid(arrive = seq.Date(as.Date("2019-07-29"), as.Date("2019-08-08"), "day"), hotel = c(900067, 9001649))
clients %>%
mutate(arrive = as.Date(arrive)) %>%
full_join(all_hotel_dates) %>%
group_by(hotel) %>%
summarise(days_without_new_clients = sum(is.na(id)))
# A tibble: 2 x 2
hotel days_without_new_clients
<dbl> <int>
1 900067 6
2 9001649 7
这篇关于如何计算不在列表中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!