Search code examples
rlistgroupingdate-difference

Group by id and store time difference(intervals) into a list


I have date-time sorted(ascending) data consisting of ID and corresponding time such as:

ID  |  Time
 1  |  2020-02-09 16:39:58
 2  |  2020-02-19 17:45:25
 1  |  2020-02-09 16:42:25
 1  |  2020-02-09 16:45:34
 2  |  2020-02-19 17:51:05
 1  |  2020-02-09 18:05:25

I would like to group by Id and then create a list corresponding to each id with time difference between intervals in minutes for that group like:

 ID  |  Time_interval
  1  |  [3,3,80]
  2  |  [6]

The nearest i've come to solving this is:

df = d %>% group_by(ID) %>% mutate(Time_interval = Time - lag(Time))

but that doesn't give me a list, it creates a separate column with lag.


Solution

  • Use summarise to store the data in a list.

    library(dplyr)
    
    d %>% 
      group_by(ID) %>% 
      summarise(Time_interval = list(as.numeric(na.omit(round(difftime(Time, 
                                     lag(Time), units = 'mins')))))) -> result
    
    result
    # A tibble: 2 x 2
    #     ID Time_interval
    #  <int> <list>       
    #1     1 <dbl [3]>    
    #2     2 <dbl [1]>    
    
    result$Time_interval
    
    #[[1]]
    #[1]  2  3 80
    
    #[[2]]
    #[1] 6
    

    data

    d <- structure(list(ID = c(1L, 2L, 1L, 1L, 2L, 1L), Time = structure(c(1581266398, 
    1582134325, 1581266545, 1581266734, 1582134665, 1581271525), class = c("POSIXct", 
    "POSIXt"), tzone = "UTC")), row.names = c(NA, -6L), class = "data.frame")