r - Date difference per ID with dplyr without hours, minutes or seconds -
i'm trying similar done in post below. using dplyr. however, avoid conversion minutes months, highest precision of date variable in current database months..
calculating time difference id
incident.id.. = c(rep("incfi0000029582",4), rep("incfi0000029587",4)) date = c("2014-09-11", "2014-10-11", "2014-11-11", "2014-12-11", "2015-01-11", "2015-02-11", "2015-03-11", "2015-04-11") df = data.frame(incident.id..,date, stringsasfactors = false) 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.. `enter code here` 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
i tried converting date variable as.date format , using dplyr functions below
library(dplyr) library(lubridate) df %>% group_by(incident.id..) %>% mutate(diff = c(0, diff(date)))
but i'm getting result:
incident.id.. date diff <chr> <date> <dbl> 1 incfi0000029582 2014-09-11 0 2 incfi0000029582 2014-10-11 30 3 incfi0000029582 2014-11-11 31 4 incfi0000029582 2014-12-11 30 5 incfi0000029587 2015-01-11 31 6 incfi0000029587 2015-02-11 31 7 incfi0000029587 2015-03-11 28 8 incfi0000029587 2015-04-11 31
i result shown below (date difference calculated using first encounter per id):
incident.id.. date diff <chr> <date> <dbl> 1 incfi0000029582 2014-09-11 0 2 incfi0000029582 2014-10-11 30 3 incfi0000029582 2014-11-11 61 4 incfi0000029582 2014-12-11 91 5 incfi0000029587 2015-01-11 0 6 incfi0000029587 2015-02-11 31 7 incfi0000029587 2015-03-11 59 8 incfi0000029587 2015-04-11 90
any idea how done? thank help.
albit
solution minimum date per id in new column , subtract each date.
min_date<-df%>%group_by(incident.id..)%>%summarise(min_date=min(date)) df<-df%>%left_join(min_date,by="incident.id..")
Comments
Post a Comment