r - how to subtract first date entry from last date entry in grouped data and control output format

前端 未结 1 475
陌清茗
陌清茗 2021-01-21 20:20

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

相关标签:
1条回答
  • 2021-01-21 21:25

    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'))
    
    0 讨论(0)
提交回复
热议问题