【问题标题】:finding overlapping time in each group of data set在每组数据集中找到重叠时间
【发布时间】:2019-07-29 17:50:04
【问题描述】:

我有分户,每户人,旅游(每个旅游包含每个人不同的行程),和模式(每个旅游中每个人的旅行方式),time_ARR开始时间,time_Dep结束时间游览。

我想找到一个关于具有汽车模式和非汽车模式的人的指标。

如果旅行时间与有模式汽车的家庭中的人有交集,则该指标为每个旅行中非汽车模式的人。

这里有一个例子来说明清楚:

  family    persons    mode    tour   start time    end time
     1      1           car     1        2:30         15:30
     1      1         non-car   2        20:00        8:30
     1      2         non-car   1        3:00         10:00
     1      3           car     1        19:10        24:00
     2      1         non-car   1        3:00         10:00
     2      2           car     1        19:10        24:00

在第一个家庭中,人 1 在他的第二次旅行中具有非汽车模式,并且与第三人有交集。

第一个家庭中的第二个人 2 也有非汽车模式,她在他的第一次旅行中也与第一个人有交集。

第二家庭人1有非汽车模式,不与其他人的汽车模式相交。 所以

  family    persons    mode    tour   start time    end time. indicator
     1      1           car     1        2:30         15:30.      NA
     1      1         non-car   2        20:00        8:30.       1
     1      2         non-car   1        3:00         10:00.      1 
     1      3           car     1        19:10        24:00.      NA
     2      1         non-car   1        3:00         10:00.      0
     2      2           car     1        19:10        24:00.      NA

它可以是 0 或 1 而不是 NA ,这根本不重要

【问题讨论】:

    标签: r dataframe


    【解决方案1】:

    查看它的一种方法是使用data.table::foverlaps,将时间用作重叠事件。

    准备数据

    dat <- read.table(header = TRUE, stringsAsFactors = FALSE, text = "
      family    persons    mode    tour   starttime    endtime
         1      1           car     1        2:30         15:30
         1      1         non-car   2        20:00        8:30
         1      2         non-car   1        3:00         10:00
         1      3           car     1        19:10        24:00
         2      1         non-car   1        3:00         10:00
         2      2           car     1        19:10        24:00")
    library(data.table)
    setDT(dat)
    
    # convert to actual timestamps ... might also use lubridate or hms packages
    dat[, c("starttime", "endtime") := lapply(.(starttime, endtime), as.POSIXct, format = "%H:%M") ]
    # assign a simple per-row id
    dat[, rowid := seq_len(.N)]
    

    不幸的是,因为您只在示例数据中列出时间,所以您有一个倒退事件,所以我将endtime 转移到“明天”:

    dat[starttime > endtime,]
    #    family persons    mode tour           starttime             endtime rowid
    # 1:      1       1 non-car    2 2019-07-29 20:00:00 2019-07-29 08:30:00     2
    dat[starttime > endtime, endtime := endtime + 86400 ]
    

    模糊重叠

    setkey(dat, starttime, endtime)
    merged <- foverlaps(dat[,.(rowid,mode,starttime,endtime)], dat[,.(rowid,mode,starttime,endtime)])
    merged[ mode == "car" & i.mode != "car", ]
    #    rowid mode           starttime             endtime i.rowid  i.mode         i.starttime           i.endtime
    # 1:     1  car 2019-07-29 02:30:00 2019-07-29 15:30:00       3 non-car 2019-07-29 03:00:00 2019-07-29 10:00:00
    # 2:     1  car 2019-07-29 02:30:00 2019-07-29 15:30:00       5 non-car 2019-07-29 03:00:00 2019-07-29 10:00:00
    # 3:     4  car 2019-07-29 19:10:00 2019-07-30 00:00:00       2 non-car 2019-07-29 20:00:00 2019-07-30 08:30:00
    # 4:     6  car 2019-07-29 19:10:00 2019-07-30 00:00:00       2 non-car 2019-07-29 20:00:00 2019-07-30 08:30:00
    

    要点是i.rowid 显示的是"non-car" 的“第二人称”,而第一人称是"car"。由此,很容易确定

    # non-car people without a "car" complement
    setdiff(dat$rowid, merged[ mode == "car" & i.mode != "car", ]$i.rowid)
    # [1] 1 4 6
    
    # non-car people with a car complement
    unique(merged[ mode == "car" & i.mode != "car", ]$i.rowid)
    # [1] 3 5 2
    
    # non-car people might be able to use these car people
    merged[ mode == "car" & i.mode != "car", ][, .(hascar = rowid, needscar = i.rowid)]
    #    hascar needscar
    # 1:      1        3
    # 2:      1        5
    # 3:      4        2
    # 4:      6        2
    

    【讨论】:

    • 你的输出我不清楚,什么是有车和需要车?
    • 它们是我分配给原始data.frame 中每一行的id。该数据的第一行具有 id 1(fam 1 人 1),并且该人有一辆可能能够帮助 id 3 的汽车(fam 1 人 2)。同样,家庭 1 人 1 可能会帮助家庭 2 人 1。所以没有。
    • 哦,我需要处理每个家庭的索引!我还需要找到我解释过的指标。我怎么能用这段代码找到它?
    • NA 分配给setdiff 命令中指定的所有ID。不过,我不知道你如何区分 0 和 1。
    • 对不起,我无法得到它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-31
    • 2018-09-28
    • 2012-12-22
    • 2021-09-25
    • 1970-01-01
    • 2016-11-22
    • 1970-01-01
    相关资源
    最近更新 更多