I was trying to reshape my data from long to wide format using pivot_wider() and spread() function. Is it possible to spread to wide dataframe group by multiple variables, i.e ccode and year in my dataframe?
current dataframe:
| ccode | industry | year | TO |
|---|---|---|---|
| 2 | agri | 2000 | 8.3 |
| 2 | agri | 2001 | 9.2 |
| 2 | mineral | 2000 | 9.1 |
| 3 | agri | 2000 | 6.1 |
| 3 | agri | 2001 | 8.1 |
| 3 | mineral | 2000 | 9 |
| 4 | agri | 2000 | 9 |
| 4 | agri | 2001 | 5.3 |
| 4 | mineral | 2000 | 8.1 |
| 5 | agri | 2000 | 8.4 |
| 5 | agri | 2001 | 4.3 |
| 5 | mineral | 2000 | 4.2 |
ideal result:
| ccode | year | agri | mineral |
|---|---|---|---|
| 2 | 2000 | 8.3 | 9.1 |
| 2 | 2001 | 9.2 | NA |
| 3 | 2000 | 6.1 | 9 |
| 3 | 2001 | 8.1 | NA |
| 4 | 2000 | 9 | 8.1 |
| 4 | 2001 | 5.3 | NA |
| 5 | 2000 | 8.4 | 9.1 |
| 5 | 2001 | 4.3 | 4.2 |
Thanks.