You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用R为每个Guest_ID动态计算无重叠的入住起止日期

酒店客人入住记录无重叠化处理流程

给定包含Guest_ID、checkin_date和boarding_days字段的酒店客人入住数据集,需为每位客人生成无入住重叠的新记录,具体步骤如下:

一、临时起止日期计算

先为每条记录计算临时起止日期:

temp_checkout_date = checkin_date + boarding_days - 1
temp_checkin_date = checkin_date

二、无重叠记录生成规则

对每位Guest_ID的记录按顺序处理,生成new_checkin_date和new_checkout_date:

  • 若无重叠:直接沿用临时计算的起止日期作为新起止日期;
  • 若存在重叠(前一条记录的temp_checkout_date ≥ 当前记录的checkin_date):将当前记录的new_checkin_date设为前一条记录的temp_checkout_date + 1,无重叠则保留原checkin_date;
  • 基于更新后的new_checkin_date动态计算new_checkout_date:new_checkout_date = new_checkin_date + boarding_days - 1;
  • 对每位客人的每一行重复上述流程。

三、示例数据集

以下是用于测试的R语言数据集:

data_df <- structure(list(Guest_ID = c(1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2), 
                          checkin_date = structure(c(17567, 17602, 17644, 17707, 17828, 17878, 17927, 17991, 18037, 18137, 18241, 18345, 18464, 18563, 18667), class = "Date"), 
                          boarding_days = c(70, 50, 50, 50, 50, 50, 50, 50, 100, 100, 100, 100, 100, 100, 100)), 
                     row.names = c(NA, -15L), class = c("tbl_df", "tbl", "data.frame"))

内容的提问来源于stack exchange,提问作者microbe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 21:47:08