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

利用顾客进出店时间记录统计每15分钟店内在店人数

R语言实现门店每15分钟在店顾客数统计

实现思路

要统计指定时间区间内每15分钟节点的在店人数,核心逻辑为:

对每个15分钟间隔的时间节点,统计满足「进店时间 ≤ 该节点 且 离店时间 ≥ 该节点」的顾客总数,即为该节点的实时在店人数。

完整可运行代码

首先修正原有代码里的笔误(entry_time定义处多余的加号),补充统计逻辑如下:

library(lubridate)
library(tidyverse)

# 模拟生成顾客进店、离店数据
set.seed(123) # 固定随机种子方便复现结果
entry_time <- ymd_hms("2021/11/9 6:00:00") + runif(n=20, min=0, max = 14 * 3600)
exit_time <- entry_time + runif(n=20, min=0, max = 5 * 3600)

# 构造顾客出入记录
tbl <- tibble(entry_time, exit_time) %>%
  arrange(entry_time) %>%
  mutate(customerID = factor(1:20))

# 1. 生成6:00到22:00的15分钟间隔时间节点序列
time_nodes <- seq(
  from = ymd_hms("2021/11/9 6:00:00"),
  to = ymd_hms("2021/11/9 22:00:00"),
  by = "15 mins"
)

# 2. 统计每个时间节点的在店人数
customer_count <- tibble(
  time_node = time_nodes,
  in_store_num = map_dbl(time_node, function(t){
    sum(tbl$entry_time <= t & tbl$exit_time >= t)
  })
)

# 查看统计结果前10行
head(customer_count, 10)

# 可选:可视化在店人数变化趋势
ggplot(customer_count, aes(x = time_node, y = in_store_num)) +
  geom_line(color = "#2c3e50", linewidth = 1) +
  geom_point(size = 2, color = "#e74c3c") +
  labs(x = "时间节点", y = "实时在店人数") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

多日数据扩展说明

如果你的数据包含多日记录,可按日期分组后,对每日单独生成时间节点执行上述统计逻辑即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 12:54:04