# how to find difference between a column and shift of another column?

I really can't understand why this question is not clear. please explain what do you need to know before closing the question. some one put comment and asked some question and I added details to make it clear! if you can't answer, plz let others answer! have start time and end time of each activity for each person. I want to find the activity duration in minutes, which is the end of the time of an activity until the start of another activity.

``````person  start-time-h  start-time-m  end-time-h    end-time-m

1           8            0            10            15
1          11          15          16            35
2           8            10          13            15
1          18            0           23            15
``````

For example, the activity of the first person is from 10:15 until 11:15 so duration is 60 min. and for the second person from 13:15 to 18:00 which is 315 minutes.

``````person  start-time-h  start-time-h  end-time-h    end-time-h     ACTDUR

1           8             0            10            15             60
1          11           15           16            35             -
2           8            10           13            15            315
2          18            0            23            15              -
``````

Thank you!

This is not only not relevant to the question but also inaccurate, questions here don't get closed by anybody because of being unclear, they get closed automatically if there is no activity on a long period of time or if the content violates the forum policies.
In the future, if you want to attract attention to your question, work in providing a proper reproducible example.

Having said that, I think this does what you want.

``````library(dplyr)
library(lubridate)

sample_df <- tibble::tribble(
~person, ~start.time.h, ~start.time.m, ~end.time.h, ~end.time.m,
1,             8,             0,          10,          15,
1,            11,            15,          16,          35,
2,             8,            10,          13,          15,
2,            18,             0,          23,          15
)

sample_df %>%
mutate(start_time = hms::hms(hours = start.time.h, minutes =  start.time.m),
end_time = hms::hms(hours = end.time.h, minutes = end.time.m)) %>%
group_by(person) %>%
mutate(ACTDUR = end_time %--% lead(start_time)/60) %>%
select(-start_time, -end_time)
#> # A tibble: 4 x 6
#> # Groups:   person [2]
#>   person start.time.h start.time.m end.time.h end.time.m ACTDUR
#>    <dbl>        <dbl>        <dbl>      <dbl>      <dbl>  <dbl>
#> 1      1            8            0         10         15     60
#> 2      1           11           15         16         35     NA
#> 3      2            8           10         13         15    285
#> 4      2           18            0         23         15     NA
``````

Created on 2020-03-07 by the reprex package (v0.3.0.9001)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.