Count consecutive number of days with condition in R

Although R is mostly known for its capability in statistical analytics and data modeling but it has great capability to data preparation and query.

Here I present a sample case which is very much required in our day to day analysis. So I came through a scenario when I have to figure out the exception when any employee has worked consecutively for 7 or 8 days without taking any off.

To count the consecutive days I have taken below dataset with Employee ID and Attendance Date.

Few data cleaning required before proceeding:

  1. Remove duplicates if any at the employee id and date level
  2. Create required dummy variable to help in counting the consecutive dates

With the use of rle function of base package we can count the consecutive dates

Output:

Here employee 1001 has consecutively marked his attendance for 7 days from 2016-12-02 to 2016-12-08. Whereas the pattern for employee 1002 is different, 1002 never marked his attendance consecutively for more than 2 days

Post your related query for customized solution.

Leave a Comment