I have a dataframe with an ID and date column. I wish to calculate the days difference between the date1 and next date for a group.
I have already tried with dplyr package , it seems wrong.
hist_trnx1 %>% group_by(card_id) %>% mutate(gap=round(c(NA,diff(purchase_date)), 1))
I would like to get the results as below
Card_ID date Diff
1. C_ID_4e6213e9bc 2017-06-25 15:33:07 NA
2: C_ID_4e6213e9bc 2017-07-15 12:10:45 20
3: C_ID_4e6213e9bc 2017-08-09 22:04:29 34
4: C_ID_4e6213e9bB 2017-03-10 10:06:26 NA #( Because of group change)
5: C_ID_4e6213e9bB 2017-04-10 01:14:19 30
6: C_ID_4e6213e9bD 2018-02-24 08:45:05 NA #( Because of group change )
7: C_ID_4e6213e9bD 2018-03-23 08:45:05 29
data
structure(list(card_id = c("C_ID_4e6213e9bc", "C_ID_4e6213e9bc",
"C_ID_4e6213e9bc", "C_ID_4e6213e9bc", "C_ID_4e6213e9bc", "C_ID_4e6213e9bc"
), purchase_date = structure(c(1498404787, 1500120645, 1502316269,
1504346786, 1489108459, 1519461905), tzone = "UTC", class = c("POSIXct",
"POSIXt"))), .Names = c("card_id", "purchase_date"), class = c("data.table",
"data.frame"), row.names = c(NA, -6L))