lundi 24 mai 2021

Create columns based on multiple other columns in R

I am trying to create multiple other columns based on three columns in my data (Company, Name, and Year).

I want to create multiple columns which give me the output as displayed in the table below. The following are the criteria for each column I want to create.

Number_Years: Number of years a person has worked with the company. 7years_Span: Number of years a person has worked with the company in the last 7 years (including the current year). Continuous_Years: Number of continuous years a person has worked with the company. Years_Gap: Number of years between that last time Person worked with the company.

Based on the columns Company, Name and Year: I want to create other columns as defined above

Company Name Year Number_Years 7years_span Continuous_Years Years_Gap
ABC John 2002 7 1 3 0
ABC John 2003 7 2 3 0
ABC John 2004 7 3 3 0
ABC Dave 2005 2 1 1 0
ABC John 2006 7 4 1 1
ABC Dave 2007 2 2 1 1
ABC John 2008 7 5 1 1
ABC John 2009 7 5 1 0
BBC Jim 2010 1 1 1 0
ABC Jim 2010 2 1 2 0
BBC Dave 2011 1 1 1 0
BBB John 2011 1 1 1 0
ABC Jim 2011 2 2 2 0
ABC John 2012 7 4 1 2

I have tried to create these columns but I have gotten nowhere near so far and I have searched on StackOverflow and other platforms with no luck. Would appreciate it if anyone can help. I hope I have provided a simple example with enough information.

Aucun commentaire:

Enregistrer un commentaire