【问题标题】:How to get people in the store at every 5 minutes?如何每 5 分钟吸引人到店?
【发布时间】:2023-03-09 03:29:01
【问题描述】:

我有一个如下的数据表:

library(data.table)
DT1<-data.table(
  id=c(1,2,3,4,3,2),
  in_time=c("2017-11-01 08:37:35","2017-11-01 09:07:44","2017-11-01 09:46:16","2017-11-01 10:32:29","2017-11-01 10:59:25","2017-11-01 13:24:12"),
  out_time=c("2017-11-01 08:45:35","2017-11-01 09:15:30","2017-11-01 10:11:16","2017-11-01 10:37:05","2017-11-01 11:45:25","2017-11-01 14:10:09")
  )

它包含有关一个人何时进入商店和离开商店的每条信息。

现在我想每 5 分钟(标准 5 分钟,如 0、5、10、15 ...60 分钟)带走一次商店。如果没有,我需要一个 0 值。

所以我尝试了

library(lubridate)
DT1[,time:=ymd_hms(in_time)]
DT1[,time:=ceiling_date(time,"5mins")]
DT1[,.N,by=list(time)]

它只给出了每次有多少人进入,但我现在被困在如何考虑 out_time。例如,id 1 在2017-11-01 08:37:35 输入并在2017-11-01 08:45:35 离开。所以他将在从 2017-11-01 08:40:00 到 5 分钟间隔的商店 2017-11-01 08:45:00 而不是2017-11-01 08:50:00 等等。

一个 id 可以重复多次,就像一个人每天多次光顾商店一样。

感谢任何帮助。

【问题讨论】:

  • id 1 是否被认为是从 2017-11-01 08:35:00 到 2017-11-01 08:40:00 在商店中?如果是,您可以使用data.table::foverlaps

标签: r dplyr data.table lubridate


【解决方案1】:

这是一个使用data.table::foverlaps的选项:

#generate intervals of 5mins
times <- seq(as.POSIXct("2017-11-01 00:00:00", format=fmt), 
    as.POSIXct("2017-11-02 00:00:00", format=fmt), 
    by="5 min")
DT2 <- data.table(in_time=times[-length(times)], out_time=times[-1L], key=c("in_time","out_time"))

#set keys before foverlaps
setkey(DT1, in_time, out_time)

#find overlaps and count distinct in each 5min interval. 
#!is.na(id) is for truncating the output for checking. to be removed in actual code 
foverlaps(DT2, DT1)[!is.na(id), uniqueN(id), .(i.in_time, i.out_time)]

如果id在每个时间间隔内都是唯一的,那么最后一行代码可以改为foverlaps(DT2, DT1)[, sum(!is.na(id)), .(i.in_time, i.out_time)]

前8行输出:

              i.in_time          i.out_time V1
 1: 2017-11-01 08:35:00 2017-11-01 08:40:00  1
 2: 2017-11-01 08:40:00 2017-11-01 08:45:00  1
 3: 2017-11-01 08:45:00 2017-11-01 08:50:00  1
 4: 2017-11-01 09:05:00 2017-11-01 09:10:00  1
 5: 2017-11-01 09:10:00 2017-11-01 09:15:00  1
 6: 2017-11-01 09:15:00 2017-11-01 09:20:00  1
 7: 2017-11-01 09:45:00 2017-11-01 09:50:00  1
 8: 2017-11-01 09:50:00 2017-11-01 09:55:00  1

数据:

library(data.table)
DT1 <- data.table(
    id=c(1,2,3,4,3,2),
    in_time=c("2017-11-01 08:37:35","2017-11-01 09:07:44","2017-11-01 09:46:16","2017-11-01 10:32:29","2017-11-01 10:59:25","2017-11-01 13:24:12"),
    out_time=c("2017-11-01 08:45:35","2017-11-01 09:15:30","2017-11-01 10:11:16","2017-11-01 10:37:05","2017-11-01 11:45:25","2017-11-01 14:10:09")
)
cols <- c("in_time", "out_time")
fmt <- "%Y-%m-%d %T"
DT1[, (cols) := lapply(.SD, as.POSIXct, format=fmt), .SDcols=cols]

【讨论】:

  • 只是好奇 findOverlaps 也能做到这一点吗?
  • 抱歉,从哪个包中找到Overlaps?
  • Bioconductor 软件包 GenomicRanges。我正在阅读源代码 https://www.r-bloggers.com/comparing-the-execution-time-between-foverlaps-and-findoverlaps/ 的基准测试。
  • @Ricky,是的,确实两者都执行相同的功能
猜你喜欢
  • 2021-04-05
  • 1970-01-01
  • 2015-07-31
  • 2021-08-29
  • 2014-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-18
相关资源
最近更新 更多