I have a data.frame called people which describes unique employees. The thing is that the journey of job roles an employee has had is expressed by a new row for each new role, where all variables stay the same except for Job_history.
employee_ID <- c('1','1','2','2','2','3','4','4')
name <- c('Adam','Adam','Ben','Ben','Ben','Chris','Dan','Dan')
Job_role <- c('Manager','Manager', 'CSO', 'CSO', 'CSO','Manager', 'CTO', 'CTO')
Job_history <- c('Manager', 'Web designer', 'CSO', 'Graduate', 'Intern', '0', 'CTO', 'Manager')
people.data <- data.frame(employee_ID, name, Job_role, Job_history)
employee_ID name Job_role Job_history
1 1 Adam Manager Manager
2 1 Adam Manager Web designer
3 2 Ben CSO CSO
4 2 Ben CSO Graduate
5 2 Ben CSO Intern
6 3 Chris Manager Manager
7 4 Dan CTO CTO
8 4 Dan CTO Manager
To further process my data I need to collapse these duplicates while preserving the job history. I would like to put the Job_history values that are currently in rows into new columns, and not have the current job repeated in the Job_history column. To visualise, I want the data.frame to look like this:
employee_ID name Job_role Job_history Job_history2
1 1 Adam Manager Web designer N/A
2 2 Ben CSO Graduate Intern
3 3 Chris Manager N/A N/A
4 4 Dan CTO Manager N/A
How do I go about this please? I have tried using duplicate() or unique() but struggle to get the values into the new columns, and in the right order (bottom to top becomes left to right for each employee).
Any help is greatly appreciated!