I have a data frame that records the daily occurrence of different activities. I would like to identify the number of days that an activity occurrence in repeated order and its duration. A week starts with day1 and ends with day7. For example in the case of id 12 the activity occurs during 7 days and duration is 11; in the case of of 123 the occurrence of activity is not consecutive as their is a gap day (day3 and day6) and in the case of id 10 the number of occurrence is 6 days and duration is 18.
Input:
id day1 day2 day3 day4 day5 day6 day7
12 2 1 2 1 1 3 1
123 0 3 0 3 3 0 3
10 0 3 3 3 3 3 3
Output:
id Duration Occurance
12 11 7
123 12 0
10 18 6
Sample data set:
structure(list(id = c(12L, 123L, 10L), day1 = c(2L, 0L, 3L),
day2 = c(1L, 3L, 3L), day3 = c(2L, 0L, 3L), day4 = c(1L,
3L, 3L), day5 = c(1L, 3L, 3L), day6 = c(3L, 0L, 3L), day7 = c(1L,
3L, 3L)), row.names = c(NA, -3L), class = c("data.table",
"data.frame"), .internal.selfref = <pointer: 0x000002a81a571ef0>)