开始使用免费开始使用

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.

本练习是课程的一部分

HR Analytics: Predicting Employee Churn in R

查看课程

练习说明

  • 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.

交互式实操练习

通过完成这段示例代码来试试这个练习。

# 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()
编辑并运行代码