简体   繁体   中英

Compare time between rows with same date/group/id in R

My dataframe is like this:

Device_id      Group  Nb_burst            Date_time      
       24          1        3   2018-09-02 10:04:04       
       24          1        5   2018-09-02 10:08:00 
       55          2        3   2018-09-03 10:14:34 
       55          2        7   2018-09-03 10:02:29 
       16          3        2   2018-09-20 08:17:11     
       16          3       71   2018-09-20 06:03:40 
       22          4       10   2018-10-02 11:33:55
       22          4       14   2018-10-02 16:22:18

I would like to know, only for the same ID, the same Group number, and the same Date, the timelag between two rows.

If timelag > 1 hour then all right keep them all.
If timelag < 1 hour then keep only the rows with the biggest Nb_burst.

Which mean a dataframe like:

Device_id      Group  Nb_burst            Date_time         
       24          1        5   2018-09-02 10:08:00
       55          2        7   2018-09-03 10:02:29 
       16          3       71   2018-09-20 06:03:40 
       22          4       10   2018-10-02 11:33:55
       22          4       14   2018-10-02 16:22:18

I tried:

    Data$timelag <- c(NA, difftime(Data$Min_start.time[-1], Data$Min_start.time[-nrow(Data)], units="hours"))

But I don't know how test only when Date, ID, and Group are the same, probably a loop. My df has 1500 rows.

Hope someone could help me. Thank you !

I'm not sure why your group 3 is not duplicated, since time difference is greater than one hour.

But, you could create two indexing variables using ave . First, the order of the Nb_burst for each grouping. Second, the tine differences for each grouping.

dat <- within(dat, {
  score <- ave(Nb_burst, Device_id, Group, as.Date(Date_time), 
               FUN=order)
  thrsh <- abs(ave(as.numeric(Date_time), Device_id, Group, as.Date(Date_time),
                   FUN=diff)/3600) > 1
})

Finally subset by rowSums .

dat[rowSums(dat[c("score", "thrsh")]) > 1,1:4]
#   Device_id Group Nb_burst           Date_time
# 2        24     1        5 2018-09-02 10:08:00
# 3        55     2        7 2018-09-03 10:14:34
# 5        16     3        2 2018-09-20 08:17:11
# 6        16     3       71 2018-09-20 06:03:40
# 7        22     4       10 2018-10-02 11:33:55
# 8        22     4       14 2018-10-02 16:22:18

Data

dat <- structure(list(Device_id = c(24L, 24L, 55L, 55L, 16L, 16L, 22L, 
22L), Group = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L), Nb_burst = c(3L, 
5L, 7L, 3L, 2L, 71L, 10L, 14L), Date_time = structure(c(1535875444, 
1535875680, 1535962474, 1535961749, 1537424231, 1537416220, 1538472835, 
1538490138), class = c("POSIXct", "POSIXt"), tzone = "")), row.names = c(NA, 
-8L), class = "data.frame")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM