I have a long dataframe that lists the top 3 employers of each occupation code (3 rows per occupation code). It looks like this.
| occcode | employer |
|---|---|
| 1 | top employer for occcode1 |
| 1 | 2nd employer for occcode 1 |
| 1 | 3rd employer for occcode 1 |
| 2 | top employer for occcode2 |
| 2 | 2nd employer for occcode 2 |
| 2 | 3rd employer for occcode 1 |
I want to reshape it so that I have one row per occupation code, and columns named "emp1", "emp2", and "emp3" that are respectively populated with the 1st-3rd employers of that occupation code.
| occcode | employer1 | employer2 | employer3 |
|---|---|---|---|
| 1 | top employer for occcode1 | 2nd employer for occcode 1 | 3rd employer for occcode 1 |
| 2 | top employer for occcode2 | 2nd employer for occode2 | 3rd employer for occcode 1 |
I previously thought using the spread() function would work. But reading the documentation and testing it out, it doesn't produce what I have in mind because it requires that the values in "employer" in the long version of the data be standardized (such that there are only 3 employer names); that's not the case because employer names vary a lot across occupation codes.
What is the best way to do reshape the data in line with what I need?