Calculating time difference by ID

后端 未结 2 1054
遇见更好的自我
遇见更好的自我 2020-12-03 23:37

I have data like this:

Incident.ID.. = c(rep(\"INCFI0000029582\",4), rep(\"INCFI0000029587\",4))
date = c(\"2014-09-25 08:39:45\", \"2014-09-25 08:39:48\", \         


        
2条回答
  •  自闭症患者
    2020-12-04 00:17

    Here is an example using dplyr and lubridate

    library(dplyr)
    library(lubridate)
    df %>%
        group_by(Incident.ID..) %>%
        mutate(diff = c(0, diff(ymd_hms(date))))
    
    Source: local data frame [8 x 3]
    Groups: Incident.ID..
    
        Incident.ID..                date    diff
    1 INCFI0000029582 2014-09-25 08:39:45       0
    2 INCFI0000029582 2014-09-25 08:39:48       3
    3 INCFI0000029582 2014-09-25 08:40:44      56
    4 INCFI0000029582 2014-10-10 23:04:00 1347796
    5 INCFI0000029587 2014-09-25 08:33:32       0
    6 INCFI0000029587 2014-09-25 08:34:41      69
    7 INCFI0000029587 2014-09-25 08:35:24      43
    8 INCFI0000029587 2014-10-10 23:04:00 1348116
    

提交回复
热议问题