Get startedGet started for free

Derive employee tenure

In this exercise, you'll derive employee tenure within the organization, i.e., the length of time an employee worked/has been in the organization.

In order to find the time difference between two dates, you can use the interval() and time_length() functions from lubridate. We have already converted the relevant date columns from character to date format.

Remember cutoff_date is used to calculate the tenure of Active employees while last_working_date is used for Inactive employees.

This exercise is part of the course

HR Analytics: Predicting Employee Churn in R

View Course

Exercise instructions

  • Derive tenure of employees in years using last_working_date, date_of_joining and cutoff_date.
  • Generate a box plot to visualize employee tenure for Active and Inactive employees.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Add tenure
emp_tenure <- emp_jhi %>%
  mutate(tenure = ifelse(status == "Active", 
                         time_length(interval(date_of_joining, ___), 
                                     "years"), 
                         ___(___(date_of_joining, ___), 
                                     "years")))

# Compare tenure of active and inactive employees
ggplot(emp_tenure, aes(x = ___, y = ___)) + 
  geom_boxplot()
Edit and Run Code