I’ve to transform my dataframe from the current to the new format (see image or structure below). I’ve no idea how I can accomplish that. I want a year for each ID, from 2013-2018 (so each ID has 6 rows, one for every year). The dates are the dates of living on that adress (entry date) and when they left that adress (end date). So each ID and year gives the zipcode and city they lived. The place the ID lived (for each year) should be were they lived the longest that year. I've already set the enddate to 31-12-2018 if they still live there (here showed with NA). Below a picture and the first 3 rows. Hopefully you guys can help me out!
Current format:
- ID (1, 1, 2)
- ZIPCODE (1234AB, 5678CD, 9012EF)
- CITY (NEWYORK, LA, MIAMI)
- ENTRY_DATE (2-1-2014, 13-3-2017, 10-11-2011)
- END_DATE (13-5-2017, 21-12-2018, 6-9-2017)
New format:
- ID (1, 1, 1, 1, 1, 1, 2)
- YEAR (2013, 2014, 2015, 2016, 2017, 2018, 2013)
- ZIPCODE (NA, 1234AB, 1234AB, 1234AB, 5678CD, 5678CD, 9012EF)
CITY (NA, NEWYORK, NEWYORK, NEWYORK, LA, LA, MIAMI)
ZIPCODE
andCITY
and usually a better way to share data is using `dput. Read about how to give a reproducible example. – Ronak Shah