2

This question is very similar to a question asked in another thread which can be found here. I'm trying to achieve something similar: within groups (events) subtract the first date from the last date. I'm using the dplyr package and code provided in the answers of this thread. Subtracting the first date from the last date works, however it does not provide satisfactory results; the resulting time difference is displayed in numbers, and there seems to be no distinction between different time units (e.g., minutes and hours) --> subtractions in first 2 events are correct, however in the 3rd one it is not i.e. should be minutes. How can I manipulate the output by dplyr so that the resulting subtractions are actually a correct reflection of the time difference? Below you will find a sample of my data (1 group only) and the code that I used:

    df<- structure(list(time = structure(c(1428082860, 1428083340, 1428084840, 
1428086820, 1428086940, 1428087120, 1428087240, 1428087360, 1428087480, 
1428087720, 1428088800, 1428089160, 1428089580, 1428089700, 1428090120, 
1428090240, 1428090480, 1428090660, 1428090780, 1428090960, 1428091080, 
1428091200, 1428091500, 1428091620, 1428096060, 1428096420, 1428096540, 
1428096600, 1428097560, 1428097860, 1428100440, 1428100560, 1428100680, 
1428100740, 1428100860, 1428101040, 1428101160, 1428101400, 1428101520, 
1428101760, 1428101940, 1428102240, 1428102840, 1428103080, 1428103620, 
1428103980, 1428104100, 1428104160, 1428104340, 1428104520, 1428104700, 
1428108540, 1428108840, 1428108960, 1428110340, 1428110460, 1428110640
), class = c("POSIXct", "POSIXt"), tzone = ""), event = c(1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3)), .Names = c("time", 
"event"), class = "data.frame", row.names = c(NA, 57L))

  df1 <- df %>%                                                     
  group_by(event) %>%                                           
  summarize(first(time),last(time),difference = last(time)-first(time))
Community
  • 1
  • 1
FlyingDutch
  • 1,100
  • 2
  • 14
  • 24

1 Answers1

3

We can use difftime and specify the unit to get all the difference in the same unit.

df %>% 
   group_by(event) %>% 
   summarise(First = first(time),
             Last = last(time) , 
             difference= difftime(last(time), first(time), unit='hour'))
akrun
  • 874,273
  • 37
  • 540
  • 662