I have a table I want to reshape/pivot. The Agency No will have duplicates as this is looking at years worth of data but they are grouped by Agency No, Fiscal Year, and Type currently. The table is provided below as well as a desired output.
| Agency No | Fiscal Year | Type | Total Gross Weight |
|---|---|---|---|
| W1000FP | 2018 | Dry | 1000 |
| W1004CSFP | 2018 | Dry | 2000 |
| W1000FP | 2018 | Produce | 500 |
| W1004CSFP | 2018 | Produce | 1000 |
| W1004DR | 2018 | Produce | 1000 |
| W1004DR | 2018 | Dry | 1000 |
| W1005DR | 2019 | Dry | 2000 |
| W1000FP | 2019 | Dry | 1000 |
| W1005DR | 2019 | Produce | 1000 |
| W1000FP | 2019 | Produce | 1000 |
Desired Output:
| Agency No | Fiscal Year | Produce Weight | Dry Weight |
|---|---|---|---|
| W1000FP | 2018 | 500 | 1000 |
| W1004CSFP | 2018 | 1000 | 2000 |
| W1004DR | 2018 | 1000 | 1000 |
| W1005DR | 2019 | 1000 | 2000 |
| W1000FP | 2019 | 1000 | 1000 |
Here is the script that I ran but did not provide the desired output:
reshape(df, idvar = "Agency No", timevar = "Type", direction = "wide"